- 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.
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 智能網(wǎng)聯(lián)汽車V2X與智能網(wǎng)聯(lián)設(shè)施I2X
- TCP/IP入門(mén)經(jīng)典(第5版)
- Hands-On Full Stack Development with Spring Boot 2 and React(Second Edition)
- 物聯(lián)網(wǎng)+BIM:構(gòu)建數(shù)字孿生的未來(lái)
- 網(wǎng)絡(luò)的琴弦:玩轉(zhuǎn)IP看監(jiān)控
- 正在爆發(fā)的互聯(lián)網(wǎng)革命
- 基于性能的保障理論與方法
- 無(wú)線傳感器網(wǎng)絡(luò)定位技術(shù)
- 沖擊:5G如何改變世界
- 組網(wǎng)技術(shù)與網(wǎng)絡(luò)管理
- 物聯(lián)網(wǎng)與智慧農(nóng)業(yè)
- 人際網(wǎng)絡(luò)
- 智慧城市中的物聯(lián)網(wǎng)技術(shù)
- 物聯(lián)網(wǎng)傳感器技術(shù)與應(yīng)用