个性化阅读
专注于IT技术分析

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);

示例:例如, 加载srcmini官方网站的命令可以写为:

driver.get("www.srcmini02.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浏览器
  • 公开网址:https://www.google.co.in/
  • 获取页面标题名称和标题长度
  • 在Eclipse控制台上打印页面标题和标题长度
  • 获取页面URL并验证它是否为所需页面
  • 获取页面源和页面源长度
  • 在Eclipse控制台上打印页面长度。
  • 关闭浏览器

出于测试目的, 我们使用” Google”搜索引擎的主页。

我们将逐步创建测试用例, 以使你全面了解如何在WebDriver中使用浏览器命令。

  • 步骤1。启动Eclipse IDE, 并打开现有的测试套件” Demo_Test”, 该套件已在WebDriver教程的” WebDriver安装”部分中创建。
  • 第2步。右键单击” src”文件夹, 然后从”新建”>”类”创建一个新的类文件。
Selenium WebDriver-浏览器命令

将你的班级名称命名为” Navigation_command”, 然后单击”完成”按钮。

Selenium WebDriver-浏览器命令

第三步让我们进入编码基础。

要自动化我们的测试方案, 首先你需要知道”如何在WebDriver中调用/启动Web浏览器?”。

注意:要在Selenium中调用浏览器, 我们必须下载特定于该浏览器的可执行文件。例如, Chrome浏览器使用名为ChromeDriver.exe的可执行文件来实现WebDriver协议。这些可执行文件将启动系统上的服务器, 该服务器负责在Selenium中运行测试脚本。

我们在本教程的后面部分中介绍了在不同的浏览器上运行测试的过程和方法。作为参考, 你可以在进行实际编码之前仔细阅读其中的每一个。

  1. 在Firefox上运行测试
  2. 在Chrome上运行测试
  3. 在Internet Explorer上运行测试
  4. 在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();
  • 为了自动化第二个测试场景, 即”获取页面标题名称和标题长度”, 我们必须将标题名称和长度分别存储在string和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个测试场景(获取页面源长度和页面源长度), 我们将页面源长度和页面源长度分别存储在字符串和int变量中。
// Storing Page Source in String variable
String pageSource = driver.getPageSource();
		 
// Storing Page Source length in Int variable
int pageSourceLength = pageSource.length();

要在控制台窗口上打印Page源代码的长度, 请遵循给定的代码片段:

// 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-浏览器命令
赞(0)
未经允许不得转载:srcmini » Selenium WebDriver-浏览器命令实例图解

评论 抢沙发

评论前必须登录!