Selenium教程

Selenium 定位策略-(按类别名称)

在本部分中,您将学习如何使用其Class属性的值来定位特定的Web元素。
让我们考虑一个测试案例,在该案例中,我们将自动化以下场景:
调用Chrome浏览器 打开URL: https://www.testandquiz.com/selenium/testing.html 单击复选框值"自动化测试"
我们将逐步创建测试用例,以使您全面了解如何使用定位器来识别和定位特定的Web元素。
Step1 。启动Eclipse IDE,并打开我们在本教程前面的课程中创建的现有测试套件" Demo_Test"。
Step2 。右键单击" src"文件夹,然后从 New> Class 创建新的类文件。
Selenium Webdriver通过类名查找策略
将类名设为" Class_Test",然后单击"完成"按钮。
Selenium Webdriver Locating Strategies By Class Name
Step3 。让我们进入编码基础。
要调用Google Chrome浏览器,我们需要下载ChromeDriver.exe文件并将系统属性" webdriver.chrome.driver"设置为ChromeDriver.exe文件的路径。我们已经在本教程的早期课程中对此进行了讨论。您还可以参考"> "在Firefox浏览器上运行测试" 了解如何下载并设置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();
    
之后,我们需要编写代码以使第二个测试场景自动化(导航到所需的URL)。
以下是示例代码,可导航到所需的URL:
    // Launch Website
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html");
    
到目前为止,完整的代码如下所示:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class_Test {
  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();
        // Launch Website
driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
  
  }
}
    
第4步。现在,我们将尝试通过使用其Class属性的值来查找所需的Web元素。在Selenium中,查找特定的Web元素涉及对其HTML代码的检查。
请按照以下步骤在示例Web页面上找到Checkbox。
打开URL: https://www.testandquiz.com/selenium/testing.html 右键单击"自动化测试"复选框,然后选择"检查元素" Selenium Webdriver Locating Strategies By Class Name 它将启动一个窗口,其中包含与Checkbox开发有关的所有特定代码。 Selenium Webdriver Locating Strategies By Class Name 选择Class属性的值,即"自动化"。 Selenium Webdriver Locating Strategies By Class Name
The使用其Class属性查找Web元素的Java语法写为:
driver.findElement(By.className (<element class>))
    
因此,为了在示例网页上定位复选框,我们将其Class属性的值用作:
driver.findElement(By.className (<"Automation">))
    
第5步。要使我们的第三个测试场景自动化,我们需要编写代码,该代码将单击Checkbox值" Automation"。
以下是示例代码单击复选框值"自动化"。
// Locate the checkbox by class Name and check it using click() function
   driver.findElement(By.className("Automation")).click();
    
因此,我们的最终测试脚本将如下所示:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class_Test {
  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();
        // Launch Website
    driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
  //Locate the checkbox by class Name and check it using click() function
      driver.findElement(By.className("Automation")).click();
  }
}
    
以下屏幕截图显示了我们测试脚本的Eclipse窗口。
Selenium Webdriver通过类名查找策略
Step6、。右键单击Eclipse代码,然后选择 Run As> Java Application 。
Selenium Webdriver Locating Strategies By Class Name
执行后,进行了上述测试脚本将启动Goggle Chrome浏览器并自动执行所有测试方案。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4