selenium Tutorial
Iniziare con il selenio
Ricerca…
Osservazioni
Il selenio è una potente libreria di comandi in più lingue (C #, Haskell, Java, JavaScript, Objective-C, Perl, PHP, Python, R e Ruby) che consentono a un programmatore di automatizzare l'interazione del browser. Questo è incredibilmente utile per gli sviluppatori che testano le applicazioni.
Il selenio offre metodi per:
- Trova un elemento in una pagina web
- Clicca su un elemento
- Invia una stringa a un elemento
- Passare a una pagina Web
- Passa a una scheda diversa nella stessa finestra del browser
- Fai uno screenshot di una pagina web
Utilizzando questi metodi, uno sviluppatore può avere un controllo automatico dei test:
- Se un elemento è in una pagina e visibile a un utente
- Un modulo di ricerca o di accesso
- Pulsanti o elementi interattivi
- Controlla i valori o gli attributi di un elemento
Il selenio gira su webdriver, simili a un normale browser Web, ma consente a Selenium di interagire con loro. In genere un test del selenio apre una nuova istanza del driver di qualunque browser lo sviluppatore stia testando, il che è sempre una lavagna pulita. In questo modo, quando si esegue un test del selenio, lo sviluppatore non deve preoccuparsi dei cookie precedenti o di una cache del browser che influisce sui risultati dell'applicazione.
Il selenio funziona anche quando si esegue un webdriver in modalità headless.
Versioni
Versione | Data di rilascio |
---|---|
3.4.0 | 2017/04/11 |
3.3 | 2017/04/07 |
3.2 | 2017/02/27 |
3.1 | 2017/02/13 |
3.0.1 | 2016/11/19 |
3.0 | 2016/10/11 |
Semplice test al selenio in Java
Sotto il codice è semplice programma java con selenio. Il viaggio del seguente codice è
- Apri il browser Firefox
- Apri la pagina di google
- Stampa il titolo della pagina di Google
- Trova la posizione della casella di ricerca
- Passa il valore come selenio nella casella di ricerca
- Invia il modulo
- Spegni il browser
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();
}
}
Semplice test al selenio in python
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()
Configurare Python Selenium tramite terminale (BASH)
Il modo più semplice è utilizzare pip e VirtualEnv . Il selenio richiede anche python 3. * .
Installa virtualenv usando:
$: pip install virtualenv
Crea / inserisci una directory per i tuoi file Selenium:
$: cd my_selenium_project
Crea un nuovo VirtualEnv nella directory per i tuoi file Selenium:
$: virtualenv -p /usr/bin/python3.0 venv
Attiva il VirtualEnv:
$: source venv/bin/active
Dovresti vedere ora vedere (venv) all'inizio di ogni riga di bash. Installare Selenium usando pip:
$: pip install selenium
Il selenio viene fornito con il driver FireFox per impostazione predefinita.
Se vuoi eseguire Selenium in google chrome, procedi anche come segue:
$: pip install chromedriver
Ora hai VirtualEnv con controllo della versione. Per assicurarti che tutto sia impostato correttamente:
Inizia Python:
$: python
Stampa fuori:
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.
Crea un nuovo webdriver (in questo caso, un chromedriver) e vai su www.google.com:
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("https://www.google.com")
Chiudi il driver e l'interprete python:
>>> driver.quit()
>>> quit()
Disattiva VirtualEnv:
$: deactivate
Se il driver = webdriver.Chrome()
linea driver = webdriver.Chrome()
errori di lancio:
- Assicurati di avere anche il browser Chrome installato. In caso contrario, il Selenium chromedriver non può accedere al binario di Chrome.
- webdriver.Chrome () può anche prendere un parametro per la tua posizione chromedriver. Se lo hai installato usando pip, prova (su mac)
driver = webdriver.Chrome("./venv/selenium/webdriver/chromedriver")
.
Semplice esempio di selenio in 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();