selenium Samouczek
Pierwsze kroki z selenem
Szukaj…
Uwagi
Selenium to potężna biblioteka poleceń w wielu językach (C #, Haskell, Java, JavaScript, Objective-C, Perl, PHP, Python, R i Ruby), które pozwalają programiście zautomatyzować interakcję przeglądarki. Jest to niezwykle przydatne dla programistów testujących aplikacje.
Selenium oferuje metody:
- Znajdź element na stronie internetowej
- Kliknij element
- Wyślij ciąg do elementu
- Przejdź do strony internetowej
- Przejdź na inną kartę w tym samym oknie przeglądarki
- Zrób zrzut ekranu strony internetowej
Korzystając z tych metod, programista może automatycznie sprawdzać testy:
- Jeśli element znajduje się na stronie i jest widoczny dla użytkownika
- Formularz wyszukiwania lub logowania
- Przyciski lub elementy interaktywne
- Sprawdź wartości lub atrybuty elementu
Selenium działa w przeglądarkach internetowych, które są podobne do zwykłej przeglądarki internetowej, ale pozwalają Selenium na interakcję z nimi. Test Selenium zwykle otwiera nową instancję sterownika w dowolnej przeglądarce, w której testuje programista, co zawsze jest czystym kontem. W ten sposób, uruchamiając test Selenium, programista nie musi się martwić o wcześniejsze pliki cookie lub pamięć podręczną przeglądarki, która ma wpływ na wyniki ich aplikacji.
Selenium działa również podczas pracy z serwerem sieciowym w trybie bezgłowym.
Wersje
Wersja | Data wydania |
---|---|
3.4.0 | 2017-04-11 |
3.3 | 2017-04-07 |
3.2 | 27.02.2017 |
3.1 | 13.02.2017 |
3.0.1 | 19.11.2016 |
3.0 | 2016-10-11 |
Prosty test selenu w Javie
Poniższy kod jest prostym programem Java używającym selenu. Podróż poniższego kodu to
- Otwórz przeglądarkę Firefox
- Otwórz stronę Google
- Wydrukuj tytuł strony Google
- Znajdź lokalizację pola wyszukiwania
- Podaj wartość jako selen w polu wyszukiwania
- Prześlij formularz
- Zamknij przeglądarkę
package org.openqa.selenium.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time
// when trying to find an element or elements if they are not immediately available.
// The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Maximize the browser window to fit into screen
driver.manage().window().maximize();
// Visit Google
driver.get("http://www.google.com");
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Selenium!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
//Close the browser
driver.quit();
}
}
Prosty test selenu w pythonie
from selenium import webdriver
# Create a new chromedriver
driver = webdriver.Chrome()
# Go to www.google.com
driver.get("https://www.google.com")
# Get the webelement of the text input box
search_box = driver.find_element_by_name("q")
# Send the string "Selenium!" to the input box
seach_box.send_keys("Selenium!")
# Submit the input, which starts a search
search_box.submit()
# Wait to see the results of the search
time.sleep(5)
# Close the driver
driver.quit()
Konfigurowanie Selenu Pythona przez terminal (BASH)
Najprostszym sposobem jest użycie pipa i VirtualEnv . Selen wymaga również pytona 3. * .
Zainstaluj virtualenv, używając:
$: pip install virtualenv
Utwórz / wprowadź katalog dla swoich plików Selenium:
$: cd my_selenium_project
Utwórz nowy VirtualEnv w katalogu dla swoich plików Selenium:
$: virtualenv -p /usr/bin/python3.0 venv
Aktywuj VirtualEnv:
$: source venv/bin/active
Powinieneś zobaczyć teraz (venv) na początku każdej linii bash. Zainstaluj Selenium za pomocą pip:
$: pip install selenium
Selenium jest domyślnie wyposażony w sterownik FireFox.
Jeśli chcesz uruchomić Selenium w Google Chrome, wykonaj również następujące czynności:
$: pip install chromedriver
Masz teraz VirtualEnv z kontrolą wersji. Aby upewnić się, że wszystko jest skonfigurowane poprawnie:
Uruchom python:
$: python
Wydruki:
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Utwórz nowy serwer WWW (w tym przypadku chromedriver) i przejdź do strony www.google.com:
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("https://www.google.com")
Zamknij sterownik i interpreter Pythona:
>>> driver.quit()
>>> quit()
Dezaktywuj VirtualEnv:
$: deactivate
Jeśli driver = webdriver.Chrome()
linii driver = webdriver.Chrome()
błędy:
- Upewnij się, że masz także zainstalowaną przeglądarkę Chrome. Jeśli nie, chromedriver Selenium nie może uzyskać dostępu do pliku binarnego Chrome.
- webdriver.Chrome () może również przyjmować parametr lokalizacji chromedriver. Jeśli zainstalowałeś go za pomocą pip, spróbuj (na komputerze Mac)
driver = webdriver.Chrome("./venv/selenium/webdriver/chromedriver")
.
Prosty przykład selenu w C #
//Create a new ChromeDriver
IWebDriver driver = new ChromeDriver();
//Navigate to www.google.com
driver.Navigate().GoToUrl("https://www.google.com");
//Find the WebElement of the search bar
IWebElement element = driver.FindElement(By.Name("q"));
//Type Hello World into search bar
element.SendKeys("Hello World");
//Submit the input
element.Submit();
//Close the browser
driver.Quit();