Selenium WebDriver - 命令


Selenium WebDriver - 命令

正如我们之前在 IDE 部分所讨论的,Selenium 命令是用于运行我们的 Selenium 测试的一组命令。

在 Selenium WebDriver 中,我们有一组完全不同的命令来执行不同的操作。由于我们将 Selenium WebDriver 与 Java 一起使用,因此命令只是用 Java 语言编写的方法

注意:java 方法是组合在一起以执行特定操作的语句的集合。

在深入了解 Selenium WebDriver 提供的命令的细节之前,我们坚持让您了解 Java 编程语言中的 Java OOP(面向对象编程)概念。您还可以参考Java 教程中提供的Java OOP 概念部分。

现在,问题来了,我们如何访问 WebDriver 提供的方法。

到目前为止,我们已经成功地在 Selenium WebDriver 中创建了我们的第一个测试脚本。因此,查看 WebDriver 提供的方法的一种可能方法是打开加载了 Selenium Webdriver jar 文件的 Eclipse IDE,为 WebDriver 创建驱动程序对象并按点键。它将向您展示 WebDriver 提供的所有可能的方法。

方法名称

要访问任何类的任何方法,我们需要创建一个类的对象,然后该对象的所有公共方法都会出现。

范围

参数是传递给方法以执行某些特定操作的参数。

返回类型

方法可以返回一个值或不返回任何内容 (void)。如果在方法之后提到了 void,则表示该方法没有返回任何值。如果它返回一个值,那么它必须显示值的类型,例如getTitle(): String

现在,我们将讨论 WebDriver 提供的各种命令。Selenium WebDriver 提供的命令大致可以分为以下几类:

  1. 浏览器命令
  2. 导航命令
  3. WebElement 命令

1. 获取网页

获取网页有两种方法:

  • 使用获取方法
driver.get("www.codingdict.com")
  • 使用导航方法
driver.navigate().to("https://codingdict.com/selenium-tutorial");

2. 定位表单并发送用户输入

driver.findElement(By.id("lst-ib")).sendKeys("codingdict tutorials");

3. 清除用户输入

clear() 方法用于清除文本框中的用户输入。

driver.findElement(By.name("q")).clear();

4. 通过任何网络元素获取数据

有时我们需要获取写在 web 元素上的文本来执行一些断言和调试。我们使用 getText() 方法来获取写入任何 Web 元素的数据。

driver.findElement(By.id("element567")).getText();

5. 执行点击事件

click() 方法用于对任何网页元素进行点击操作。

driver.findElement(By.id("btnK")).click();

6. 在浏览器历史记录中向后导航

driver.navigate().back();

7. 在浏览器历史记录中向前导航

driver.navigate().forward();

8. 刷新/重新加载网页

driver.navigate().refresh();

9. 关闭浏览器

driver.close();

10.关闭浏览器和其他与驱动程序相关的所有其他窗口

driver.quit();

11. 在 Windows 之间移动

driver.switchTo().window("windowName");

13. 在帧之间移动

driver.switchTo().frame("frameName");

14.Drag and Drop

使用 Action 类执行拖放操作。

WebElement element = driver.findElement(By.name("source"));  
WebElement target = driver.findElement(By.name("target"));  

(new Actions(driver)).dragAndDrop(element, target).perform();

让我们考虑一个示例测试脚本,它将涵盖大多数常用的 WebDriver 命令。

出于我们的测试目的,我们在 URL 下使用了一个虚拟网页:

https://www.testandquiz.com/selenium/testing.html

网页的默认界面如下所示:

Selenium WebDriver 命令

您还可以将这个虚拟网页用于 Selenium 测试实践。

首先,您需要为您愿意自动化测试场景的浏览器下载浏览器驱动程序。我们已经在本教程的前面部分讨论了在不同浏览器上执行 Selenium 测试脚本。

对于此测试,我们使用 Firefox Gecko 驱动程序在 Firefox 浏览器上自动化我们的测试场景。

下面是带有嵌入注释的示例测试脚本。

import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.firefox.FirefoxDriver;  import org.openqa.selenium.remote.DesiredCapabilities;  import org.openqa.selenium.support.ui.Select;    public class Second {        public static void main(String[] args) {                      // System Property for Gecko Driver       System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );                     // Initialize Gecko Driver using Desired Capabilities Class          DesiredCapabilities capabilities = DesiredCapabilities.firefox();          capabilities.setCapability("marionette",true);          WebDriver driver= new FirefoxDriver(capabilities);                    // Launch Website       driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");                // Fetch the text "This is sample text." and print it on console          // Use the class name of the div to locate it and then fetch text using getText() method       String sampleText = driver.findElement(By.className("col-md-12")).getText();       System.out.println(sampleText);                      // Use the linkText locator method to find the link and perform click using click() method       driver.findElement(By.linkText("This is a link")).click();                   // Click on the textbox and send value       driver.findElement(By.id("fname")).sendKeys("codingdict");                 // Clear the text written in the textbox       driver.findElement(By.id("fname")).clear();                    // Click on the Submit button using click() command       driver.findElement(By.id("idOfButton")).click();             // Locate the radio button by id and check it using click() function       driver.findElement(By.id("male")).click();                    // Locate the checkbox by cssSelector and check it using click() function       driver.findElement(By.cssSelector("input.Automation")).click();                        // Use Select class for selecting value from dropdown      Select dropdown = new Select(driver.findElement(By.id("testingDropdown")));      dropdown.selectByVisibleText("Automation Testing");                 // Close the Browser               driver.close();            }    }