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提供的所有可能方法。
考虑一个由Eclipse显示的建议示例,以理解WebDriver提供的方法的语法。
方法名称
要访问任何类的任何方法,我们需要创建一个类的对象,然后该对象的所有公共方法都会出现。
参数
参数是传递给方法以执行某些特定操作的参数。
返回类型
方法可以返回值或不返回任何值(无效)。如果在方法后面提到了void,则表示该方法未返回任何值。如果返回值,则必须显示值的类型,例如 getTitle(): 字符串。
现在,我们将讨论WebDriver提供的各种命令。 Selenium WebDriver提供的命令可以大致分为以下几类:
浏览器命令
导航命令
WebElement命令
下面是WebDriver中最常用的一些Selenium命令:
1、提取网页
有两种提取网页的方法:
使用Get方法
driver.get("www.lidihuo.com")
使用导航方法
driver.navigate().to("https://lidihuo.com/selenium-tutorial");
2、查找表单并发送用户输入
driver.findElement(By.id("lst-ib")).sendKeys("lidihuo tutorials");
3、清除用户输入
clear()方法用于清除文本框中的用户输入。
driver.findElement(By.name("q")).clear();
4、通过任何Web元素获取数据
有时,我们需要获取通过Web元素编写的文本,以执行一些断言和调试。我们使用getText()方法来获取通过任何Web元素写入的数据。
driver.findElement(By.id("element567")).getText();
5、执行Click事件
click()方法用于对任何Web元素执行点击操作。
driver.findElement(By.id("btnK")).click();
6、向后浏览浏览器历史记录
driver.navigate().back();
7、导航浏览器历史记录
driver.navigate().forward();
8、刷新/重新加载网页
driver.navigate().refresh();
9、关闭浏览器
10、关闭浏览器和其他与驱动程序相关的所有其他窗口
11、在Windows之间切换
driver.switchTo().window("windowName");
13、在框架之间移动
driver.switchTo().frame("frameName");
14、拖放
使用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 Testing练习。
首先,您需要为愿意自动执行测试场景的浏览器下载浏览器驱动程序。在本教程的前面部分中,我们已经讨论了在不同浏览器上执行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("lidihuo");
// 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();
}
}