Selenium 中的对象库(XML 和属性文件)

什么是对象库?

对象库是所有对象的通用存储位置。在 Selenium WebDriver 上下文中,对象通常是用于唯一标识 Web 元素的定位器。

使用对象库的主要优点是将对象与测试用例分离。如果某个 Web 元素的定位器值发生变化,只需更改对象库,而无需更改所有使用该定位器的测试用例。维护对象库增加了框架实现的模块化。

Selenium Web Driver 中对象库的类型

Selenium WebDriver 默认不提供内置对象库。但是,可以使用键值对方法构建对象库,其中键指向给定对象的名称,值指向用于唯一标识网页中对象的属性。

以下是在 Selenium WebDriver 中可以创建的对象库类型。

  1. 使用属性文件的对象库
  2. 使用 XML 文件的对象库

使用属性文件的 Selenium Web Driver 对象库

在这种方法中,属性文件是一个文本文件,其中数据以键值对的形式存储。下面的教程将涉及以下主题。

步骤 1) 在 Eclipse 中创建属性文件

  1. 首先,需要在 Eclipse 中创建下面的 Java 项目结构。项目名称和包名称可以是任何有效的名称。

Creating A Properties File In Eclipse

  1. 右键单击主项目文件夹,选择新建->其他

Creating A Properties File In Eclipse

  1. 在下一个窗口中,选择通用->文件,然后单击“下一步”按钮

Creating A Properties File In Eclipse

  1. 在新文件资源窗口中提供一个有效的带“.properties”扩展名的文件名,然后单击“完成”按钮

Creating A Properties File In Eclipse

  1. 项目结构中应显示一个名为“application.properties”的文件

Creating A Properties File In Eclipse

步骤 2) 将数据存储到属性文件

  1. 数据以键值对的形式存储在属性文件中,其中键在文件中是唯一的。
  2. 我们将尝试使用属性文件通过定位器值来识别 Web 元素。
  3. 在 Eclipse 中打开 application.properties 文件并存储以下数据
MobileTesting=//a[text()='MOBILE TESTING']
EmailTextBox = philadelphia-field-email
SignUpButton = philadelphia-field-submit

 Storing Data Onto Properties File

4) 对于本教程,使用以下演示网站

https://demo.guru99.com/test/guru99home/。这是测试场景

  • 使用 XPATH 单击“移动测试”链接
  • 返回
  • 使用 ID 在电子邮件文本框中输入数据
  • 使用 ID 单击“注册”按钮

步骤 3) 从属性文件读取数据

  1. 可以使用 Java 中提供的内置 Properties 类从属性文件读取数据。
  2. 首先,需要如下创建 Properties 类的对象
  Properties obj = new Properties();
  1. 我们需要创建一个 FileInputStream 类的对象,并传入属性文件的路径
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties");
  1. 可以使用 Java 中 Properties 类提供的 load 方法从属性文件读取数据。下面的代码演示了 load 方法的使用。
Properties obj = new Properties();
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties");
obj.load(objfile);
String mobileTesting = obj.getProperty("MobileTesting");

字符串“mobileTesting”将包含用于在网页中标识“移动测试”链接的 XPATH。

步骤 4) 在测试脚本中使用属性文件

属性文件可以在测试脚本中使用,通过从属性文件读取数据并将其作为参数传递给 findElement 方法。下面的代码演示了在测试脚本中使用从属性文件读取的数据。

driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click();
driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("testguru99@gmail.com");								
driver.findElement(By.id(obj.getProperty("SignUpButton"))).click();

以下是上述测试场景中使用的完整代码。

package com.objectrepository.demo;		

import java.io.FileInputStream;		
import java.io.FileNotFoundException;		
import java.io.IOException;		
import java.util.Properties;		

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.chrome.ChromeDriver;		

public class DemoOR {				

public static void main(String[] args) throws IOException {										
	
// Create WebDriver Instance		
    WebDriver driver;			
    System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");					
    driver = new ChromeDriver();					
    driver.get("https://demo.guru99.com/test/guru99home/");					
    driver.manage().window().maximize();			
// Load the properties File		
    Properties obj = new Properties();					
    FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\application.properties");									
    obj.load(objfile);					
// Nagigate to link Mobile Testing and Back		
    driver.findElement(By.xpath(obj.getProperty("MobileTesting"))).click();							
    driver.navigate().back();			
// Enter Data into Form		
    driver.findElement(By.id(obj.getProperty("EmailTextBox"))).sendKeys("testguru99@gmail.com");									
    driver.findElement(By.id(obj.getProperty("SignUpButton"))).click();							
  }		

}		

使用 XML 文件的 Selenium WebDriver 对象库

XML 代表可扩展标记语言。XML 文件使用文档对象模型 (DOM) 作为基本结构。XML 文件格式将复制构建网页所用的 HTML 格式。以下是将涵盖的主题列表。

步骤 1) 在 Eclipse 中创建 XML 文件

  1. 需要在 Eclipse 中创建下面的 Java 项目结构。

Creating An XML File In Eclipse

  1. 右键单击项目文件夹,选择新建->其他

Creating An XML File In Eclipse

  1. 在 XML 文件夹中选择 XML 文件,然后单击“下一步”按钮

Creating An XML File In Eclipse

  1. 输入有效的 XML 文件名,然后单击“完成”按钮

Creating An XML File In Eclipse

  1. XML 文件将添加到项目文件夹中,如下图所示

Creating An XML File In Eclipse

步骤 2) 将数据存储到 XML 文件

数据可以以文档对象模型 (DOM) 的形式存储在 XML 文件中。为简单起见,我们可以使用下面的测试场景作为示例。

  • 使用 XPATH 单击“移动测试”链接
  • 返回主页
  • 使用 ID 在电子邮件文本框中输入数据
  • 使用 ID 单击“注册”按钮

以下是要使用的 XML 文件格式。

<menu>     
      <mobiletesting>//a[text()='MOBILE TESTING']</mobiletesting>  
      <email> philadelphia-field-email</email> 
      <signup> philadelphia-field-submit </signup>     
 </menu>

将上述 XML 代码存储在 properties.xml 中

Storing Data OnTo XML File

在设计选项卡中,您将看到

Storing Data OnTo XML File

步骤 3) 从 XML 文件读取数据

1. 可以使用 Java 中内置的“dom4j”类从 XML 文件读取数据。请注意,在继续编码之前,您需要将以下 JAR 文件添加到项目的构建路径中。

  • jaxen.jar
  • dom4j-1.6.jar

2. 下面是用于从 XML 文件读取数据的代码。

		File inputFile = new File(System.getProperty("user.dir") +"\\properties.xml");
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputFile);
        String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText();
        String emailTextBox = document.selectSingleNode("//menu/email").getText();
        String signUpButton = document.selectSingleNode("//menu/signup").getText();

3. 首先,我们需要创建一个 File 对象,并将其作为参数传递给 SAXReader 类的“read”方法。成功读取 XML 文件数据后,我们可以使用“selectSingleNode”方法访问 XML 文档的各个节点。

步骤 4) 在测试脚本中使用 XML 文件

XML 文件可以在测试脚本中使用,通过从 XML 文件读取数据并将其作为参数传递给 findElement 方法。下面的代码演示了在测试脚本中使用从 XML 文件读取的数据。

driver.findElement(By.xpath(mobileTesting)).click();
driver.findElement(By.id(emailTextBox)).sendKeys("testguru99@gmail.com");
driver.findElement(By.id(signUpButton)).click();

下面的代码演示了在 Selenium WebDriver 中使用 XML 文件

package com.objectrepository.demo;		
import java.io.*;		
import java.util.*;		
import org.dom4j.*;		
import org.dom4j.io.SAXReader;		
import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.chrome.ChromeDriver;		

public class DemoORXML {				

public static void main(String[] args) throws DocumentException {										
// Creating WebDriver Instance		
    WebDriver driver;			
    System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");					
    driver = new ChromeDriver();					
    driver.get("https://demo.guru99.com/test/guru99home/");					
    driver.manage().window().maximize();			
// Reading XML File    		
    File inputFile = new File(System.getProperty("user.dir") +"\\properties.xml");									
    SAXReader saxReader = new SAXReader();					
    Document document = saxReader.read(inputFile);							
    String mobileTesting = document.selectSingleNode("//menu/mobiletesting").getText();							
    String emailTextBox = document.selectSingleNode("//menu/email").getText();							
    String signUpButton = document.selectSingleNode("//menu/signup").getText();							

//Navigating to Mobile Testing and back		
    driver.findElement(By.xpath(mobileTesting)).click();					
    driver.navigate().back();			
//Entering Form Data		
driver.findElement(By.id(emailTextBox)).sendKeys("testguru99@gmail.com");						
driver.findElement(By.id(signUpButton)).click();				

	}		
}		

下载 WebDriver Eclipse 项目

摘要

  • 对象库是所有对象的通用存储位置
  • Selenium WebDriver 默认不提供内置对象库
  • 您可以在 Selenium 中创建 2 种类型的对象库
    1. 使用属性文件的对象库
    2. 使用 XML 文件的对象库
  • 属性文件是一个文本文件,其中数据以键值对的形式存储
  • XML 文件格式将复制构建网页所用的 HTML 格式。