Selenium教程

Selenium WebDriver浏览器命令

WebDriver最基本的浏览器操作包括打开浏览器;执行一些任务,然后关闭浏览器。
给出了一些最常用的Selenium WebDriver浏览器命令。

1、获取命令

方法:
get(String arg0) : void
在WebDriver中,此方法将在现有浏览器窗口中加载一个新网页。它接受 String 作为参数,并返回 void、
用于加载新网页的相应命令可以写为:
driver.get(URL);   
// Or can be written as
   
String URL = "URL";
driver.get(URL);
示例: 例如,加载lidihuo官方网站的命令可以写为:
driver.get("www.lidihuo.com")

2、获取标题命令

方法:
getTitle(): String
在WebDriver中,此方法获取当前网页的标题。它不接受任何参数并返回一个字符串。
获取当前页面标题的相应命令可以写为:
driver.getTitle();  
     
  // Or can be written as
   
 String Title = driver.getTitle();

3、获取当前URL命令

方法:
getCurrentUrl(): String
在WebDriver中,此方法获取表示当前网页的当前URL的字符串。它不接受任何参数作为参数并返回String值。
获取表示当前URL的字符串的相应命令可以写为:
driver.getCurrentUrl();
   
  //Or can be written as
     
  String CurrentUrl = driver.getCurrentUrl(); 

4、获取页面源命令

方法:
getPageSource(): String
在WebDriver中,此方法返回当前浏览器中加载的当前网页的源代码。它不接受任何参数作为参数,并返回 String 值。
获取当前网页源代码的相应命令可以写为:
driver.getPageSource();
   
  //Or can be written as 
  String PageSource = driver.getPageSource();

5、关闭命令

方法:
close(): void
此方法终止当前由WebDriver操作的当前浏览器窗口。如果当前窗口是WebDriver唯一操作的窗口,则它也将终止浏览器。此方法不接受任何参数作为参数,并返回 void、
终止浏览器窗口的相应命令可以写为:
driver.close();

6、退出命令

方法:
quit(): void
此方法终止由WebDriver操作的所有窗口。它终止所有选项卡以及浏览器本身。它不接受任何参数作为参数并返回void。
终止所有窗口的相应命令可以写为:
driver.quit();
让我们考虑一个示例测试脚本,该脚本将覆盖WebDriver提供的大多数浏览器命令。
在此示例测试中,我们将自动执行以下测试方案:
调用Chrome浏览器 打开URL: https://www.google.co.in/ 获取页面标题名称和标题长度 在Eclipse控制台上打印页面标题和标题长度 获取页面URL并验证它是否为所需页面 获取页面源和页面源长度 在Eclipse控制台上打印页面长度。 关闭浏览器
出于测试目的,我们使用" Google"搜索引擎的首页。
我们将逐步创建测试用例,以使您全面了解如何在WebDriver中使用浏览器命令。
第1步。。启动Eclipse IDE并打开我们在 WebDriver安装中创建的现有测试套件" Demo_Test"。 WebDriver教程的a>部分。 第二步。。右键单击" src"文件夹,然后从 New> Class 创建一个新的Class文件。 Selenium WebDriver-浏览器命令
将您的类名称命名为" Navigation_command",然后单击"完成"按钮。
Selenium WebDriver-浏览器命令
Step3、让我们开始编码。
要使我们的测试场景自动化,首先您需要知道"如何在WebDriver中调用/启动Web浏览器?"
注意: 要在Selenium中调用浏览器,我们必须下载特定于该浏览器的可执行文件。例如,Chrome浏览器使用名为ChromeDriver.exe的可执行文件来实现WebDriver协议。这些可执行文件将启动系统上的服务器,该服务器负责在Selenium中运行测试脚本。
在本教程的后续部分中,我们介绍了在不同的浏览器上运行测试的过程和方法。作为参考,您可以在进行实际编码之前先仔细阅读其中的每一个。
在Firefox上运行测试 在Chrome上运行测试 在Internet Explorer上运行测试 在Safari上运行测试 要调用Google Chrome浏览器,我们需要下载ChromeDriver.exe文件,并将系统属性设置为ChromeDriver.exe文件的路径。我们已经在本教程的早期课程中对此进行了讨论。您还可以参考" 在Chrome浏览器上运行测试",以了解如何下载和设置Chrome驱动程序的系统属性。
以下是为Chrome驱动程序设置系统属性的示例代码:
// System Property for Chrome Driver 
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
此后,我们必须使用ChromeDriver类初始化Chrome驱动程序。
以下是使用ChromeDriver类初始化Chrome驱动程序的示例代码:
// Instantiate a ChromeDriver class.  
WebDriver driver=new ChromeDriver();
结合以上两个代码块,我们将获得代码段以启动Google Chrome浏览器。
// System Property for Chrome Driver 
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
        
// Instantiate a ChromeDriver class.  
WebDriver driver=new ChromeDriver();
要自动化第二个测试场景,即"获取页面标题名称和标题长度",我们必须将标题名称和长度分别存储在字符串和int变量中。
以下是执行此操作的示例代码:
// Storing Title name in the String variable
String title = driver.getTitle();
   
// Storing Title length in the int variable
int titleLength = driver.getTitle().length();
要在控制台窗口中打印页面标题名称和标题长度,请遵循给定的代码片段:
// Printing Title & Title length in the Console window
System.out.println("Title of the page is : " + title);
System.out.println("Length of the title is : "+ titleLength);
下一个测试方案需要获取URL并根据实际URL进行验证。
首先,我们将当前URL存储在String变量中:
// Storing URL in String variable
String actualUrl = driver.getCurrentUrl();
验证当前URL为实际URL:
if (actualUrl.equals("https://www.google.co.in"))
{
System.out.println("Verification Successful-The correct Url is opened.");
}
Else
{
System.out.println("Verification Failed-An incorrect Url is opened.");
}
要自动执行第6个sups测试场景(获取页面源和页面源长度),我们将页面源和页面源长度存储在 string 和 int 变量。
// Storing Page Source in String variable
String pageSource = driver.getPageSource();
     
// Storing Page Source length in int variable
int pageSourceLength = pageSource.length();
要在控制台窗口上打印页面源的长度,请遵循给定的代码片段:
// Printing length of the Page Source on console
System.out.println("Total length of the Page Source is : " + pageSourceLength);
最后,给定的代码段将终止该过程并关闭浏览器。
driver.close(); 
将上面所有代码块组合在一起,我们将获得执行测试脚本" Web_command"所需的源代码。
最终的测试脚本将如下所示:
(我们在每个部分都嵌入了注释,以清楚地说明步骤)
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Web_command {
public static void main(String[] args) {
    
// System Property for Chrome Driver 
System.setProperty("webdriver.chrome.driver","D:\\ChromeDriver\\chromedriver.exe");
        
// Instantiate a ChromeDriver class.  
WebDriver driver=new ChromeDriver();
    
// Storing the Application Url in the String variable
String url = ("https://www.google.co.in/");
   
//Launch the ToolsQA WebSite
driver.get(url);
   
// Storing Title name in the String variable
String title = driver.getTitle();
   
// Storing Title length in the int variable
int titleLength = driver.getTitle().length();
   
// Printing Title & Title length in the Console window
System.out.println("Title of the page is : " + title);
System.out.println("Length of the title is : "+ titleLength);
  
// Storing URL in String variable
String actualUrl = driver.getCurrentUrl();
   
if (actualUrl.equals("https://www.google.co.in/")){
System.out.println("Verification Successful-The correct Url is opened.");
}
else{
System.out.println("Verification Failed-An incorrect Url is opened.");
     }
   
// Storing Page Source in String variable
String pageSource = driver.getPageSource();
     
// Storing Page Source length in int variable
int pageSourceLength = pageSource.length();
     
// Printing length of the Page Source on console
System.out.println("Total length of the Pgae Source is : " + pageSourceLength);
     
//Closing browser
 driver.close(); 
}
}
要在Eclipse窗口上运行测试脚本,请右键单击屏幕,然后单击
运行方式→Java应用程序
Selenium WebDriver-浏览器命令
执行后,测试脚本将启动chrome浏览器并使所有测试场景自动化。控制台窗口将显示打印命令的结果。
Selenium WebDriver-浏览器命令
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4