- 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.
- MERN Quick Start Guide
- WordPress 5 Complete
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 計算機網(wǎng)絡與通信(第2版)
- 通信十年:擁抱互聯(lián)網(wǎng)
- Getting Started with Memcached
- 移動物聯(lián)網(wǎng):商業(yè)模式+案例分析+應用實戰(zhàn)
- 云工廠:開啟中國制造云時代
- 全聯(lián)網(wǎng)標識服務
- 智慧城市中的物聯(lián)網(wǎng)技術
- 區(qū)塊鏈技術與應用:打造分布式商業(yè)新生態(tài)
- 智能物聯(lián)安防視頻技術基礎與應用
- 物聯(lián)網(wǎng)與智能制造
- EtherCAT工業(yè)以太網(wǎng)應用技術
- 趣學CCNA:路由與交換(第2版)