📜  Selenium WebDriver-浏览器命令

📅  最后修改于: 2020-11-06 04:49:15             🧑  作者: Mango

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

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

driver.get("www.javatpoint.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”文件夹,然后从“新建”>“类”创建一个新的类文件。

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

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

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

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

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

  • 要调用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();

要在控制台窗口中print页面标题名称和标题长度,请遵循给定的代码片段:

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

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

// 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应用程序

执行后,测试脚本将启动chrome浏览器并自动执行所有测试场景。控制台窗口将显示print命令的结果。