1. Ejemplos testing:
Cakes example:
package prueba.Selenium2;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestXPath {
private WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize(); //maximizo la ventana
}
@Test
public void CheeseCake() {
//entra en esta pagina
driver.get("http://www.cheesecake.com/");
//Seleccionar una pestaña, en este caso "cakes"
driver.findElement(By.xpath("/html/body/form/div[5]/div/div/div/div/di
v/div/ul/li[8]/a/span")).click();
//Selecciona una tarta
driver.findElement(By.xpath("/html/body/form/div[5]/div/div/div[3]/div
[3]/div/div[2]/div/table/tbody/tr/td/div/div[2]/a")).click();
//Escribe el valor de codigo postal
driver.findElement(By.xpath("id('ContentPlaceHolder1_txtZip')")).sendK
eys("28080");
//Creo el elemento e para usar en el combo
WebElement e=
driver.findElement(By.xpath("html/body/form/div[5]/div[1]/div[1]/div[4]/div/d
iv[2]/table[1]/tbody/tr[5]/td/select/option[4]"));
//Desplegamos el desplegable
driver.findElement(By.xpath("/html/body/form/div[5]/div/div/div[4]/div
/div[2]/table/tbody/tr[5]/td/select")).sendKeys(e.getText());
//Seleccionamos el campo correspondiente a "hospital" del
desplegable
2. driver.findElement(By.xpath("/html/body/form/div[5]/div/div/div[4]/div
/div[2]/table/tbody/tr[5]/td/select/option[4]")).click();
//Pinchamos sobre "add to cart"
driver.findElement(By.xpath("/html/body/form/div[5]/div/div/div[4]/div
/div[2]/table[2]/tbody/tr/td/a/span")).click();
}
@After
public void tearDown() {
driver.close();
}
ElCorteIngles example:
package prueba.Selenium2;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestXpath {
private WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize(); //maximizo la ventana
}
@Test
public void corteIngles(){
//Entro a esta pagina
driver.get("http://www.elcorteingles.es/");
//Hago click en Lego
driver.findElement(By.xpath("/html/body/div[5]/div/div[4]/div/div/div/
a[3]/div/span")).click();
//Selecciono un producto
driver.findElement(By.xpath("/html/body/div[5]/div[3]/div/ul/li[3]/h2/
a")).click();
//Presiono sobre el boton agregar a la cesta
driver.findElement(By.xpath("/html/body/div[5]/div[4]/div[4]/ul/li/div
[4]/form/div[3]/a")).click();
}
@After
public void tearDown() {
3. driver.close();
}
}
Amazon example:
package prueba.Selenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestXpath {
private WebDriver driver;
@Before
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize(); //maximizo la ventana
}
@Test
public void corteIngles(){
driver.get("http://www.amazon.co.uk/");
//Selecciono el producto que deseo comprar
driver.findElement(By.xpath("/html/body/header/div/div[2]/div[2]/div[3
]/form/div[2]/div/input")).sendKeys("books");;
//press find
driver.findElement(By.xpath("/html/body/header/div/div[2]/div[2]/div[3
]/form/div/input")).click();
//Select book
driver.findElement(By.xpath("/html/body/div/div/div[4]/div[2]/div/div[
4]/div/div/ul/li/div/div/div/div[2]/div[2]/div/div/a/h3")).click();
//Add to basket
driver.findElement(By.xpath("id('add-to-cart-
button')")).click();
//proceed to checkout
driver.findElement(By.xpath("id('hlb-ptc-btn-
native')")).click();
}
@After
public void tearDown() {
driver.close();