- Selenium WebDriver 3 Practical Guide
- Unmesh Gundecha Satya Avasarala
- 207字
- 2021-08-13 16:08:05
Locating WebElements using WebDriver
Let's start this section by automating the Search feature from the Homepage of the demo application, http://demo-store.seleniumacademy.com/, which involves navigating to the homepage, typing the search text in the textbox, and executing the search. The code is as follows:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
public class SearchTest {
WebDriver driver;
@BeforeMethod
public void setup() {
System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
driver = new ChromeDriver();
driver.get("http://demo-store.seleniumacademy.com/");
}
@Test
public void searchProduct() {
// find search box and enter search string
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("Phones");
WebElement searchButton =
driver.findElement(By.className("search-button"));
searchButton.click();
assertThat(driver.getTitle())
.isEqualTo("Search results for: 'Phones'");
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
As you can see, there are three new things that are highlighted, as follows:
WebElement searchBox = driver.findElement(By.name("q"));
They are the findElement() method, the By.name() method, and the WebElement interface. The findElement() and By() methods instruct WebDriver to locate a WebElement on a web page, and once found, the findElement() method returns the WebElement instance of that element. Actions, such as click and type, are performed on a returned WebElement using the methods declared in the WebElement interface, which will be discussed in detail in the next section.
- 智慧城市:大數(shù)據(jù)、互聯(lián)網(wǎng)時(shí)代的城市治理(第4版)
- 面向物聯(lián)網(wǎng)的CC2530與傳感器應(yīng)用開發(fā)
- Spring Cloud微服務(wù)架構(gòu)進(jìn)階
- 互聯(lián)網(wǎng)基礎(chǔ)資源技術(shù)與應(yīng)用發(fā)展態(tài)勢(shì)(2021—2023)
- Socket.IO Real-time Web Application Development
- 大話社交網(wǎng)絡(luò)
- 圖解手機(jī)元器件維修技巧
- 物聯(lián)網(wǎng)長距離無線通信技術(shù)應(yīng)用與開發(fā)
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- 網(wǎng)管工具使用與技巧大全
- 沖擊:5G如何改變世界
- 深入理解計(jì)算機(jī)網(wǎng)絡(luò)
- Qt5 Python GUI Programming Cookbook
- 人際網(wǎng)絡(luò)
- Corona SDK Application Design