Step 1. Download Tools
Step 2. Start a Java Project From Eclipse
- Right click the project and go to the bottom to open "properties.
- Choose Libraries and add external JARs
- Copy your executable chromedriver to Java Project folder
- In the following example, we put it under "./drivers"
- In MAC, you need to manually "chmod +x" or "chmod 755"
- In PC, the file name has ".exe" suffix
- Go to the "src" folder, right click to add new class
- Insert the following codes
- When you run, run as JUnit Test.
package com.yourdomain.topfolder.subfolder; import java.util.List; import ... 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.chrome.ChromeDriver; public class myDemo { private WebDriver driver; @Before public void setUp() { System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); driver = new ChromeDriver(); // launch the browser driver.get("http://www.ebay.com"); } @After public void tearDown() { driver.quit(); // close the browser } @Test public void testById() { ... } }
Step 3. Use Inspect to find elements for testing
WebElement elem = driver.findElement(By.id("title")); assertEquals("Hello World", elem.getText()); Listelems = driver.findElements(By.tagName("h1")); assertEquals("Hello World", elems.get(0).getText()); // Pay attention to which page you are // In home page WebElement input = driver.findElement(By.id("twotabsearchtextbox")); input.clear(); input.sendKeys("selenium webdriver books"); WebElement searchIcon = driver.findElement(By.className("nav-input")); searchIcon.click(); WebElement searchButton = driver.findElement(By.xpath( “/html/body/div[2]/header/div/div[1]/div[3]/ div/form/div[2]/div/ input”)); searchButtom.click(); System.out.println(driver.findElement(By.id("s-result-count")).getText()); // Now I am in the result page! WebElement status = driver.findElement(By.id("s-result-count")); System.out.println(status.getText()); assertEquals( "1-16 of 65 results for \"selenium webdriver books\"", status.getText() );
No comments:
Post a Comment