The function of using the object library is to store the positioning method and positioning expression of the elements on the test page in the configuration file, so as to separate the positioning data from the program. Custom modification and configuration.
Take http://mail.sohu.com as an example below.
To achieve the separation of positioning and data, it needs to be carried out in three parts:
The first step: implement the tool class Object tool class for the test program to call.
package cn.om.webdriverapi; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.openqa.selenium.By; public class ObjectMap { Properties properties; public ObjectMap(String propFile){ properties=new Properties(); try { FileInputStream in=new FileInputStream(propFile); properties.load(in); in.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("Error reading object file"); e.printStackTrace (); } } public By getLocator(String ElementNameInpropFile) throws Exception{ //According to the variable ElementNameInpropFile, read the corresponding configuration object from the property configuration file String locator=properties.getProperty(ElementNameInpropFile); /* Store the locator type in the configuration object to the locatorType variable * Store the locator expression in the locatorValue variable */ String locatorType=locator.split(":")[0]; String locatorValue=locator.split(":")[1]; System.out.println("Location type obtained: "+locatorType+"\tLocation expression obtained"+locatorValue); if(locatorType.toLowerCase().equals("id")) return By.id(locatorValue); else if(locatorType.toLowerCase().equals("name")) return By.name(locatorValue); else if(locatorType.toLowerCase().equals("classname")||locatorType.toLowerCase().equals("class")) return By.className(locatorValue); else if(locatorType.toLowerCase().equals("tag")||locatorType.toLowerCase().equals("tagname")) return By.tagName(locatorValue); else if(locatorType.toLowerCase().equals("linktext")||locatorType.toLowerCase().equals("link")) return By.linkText(locatorValue); else if(locatorType.toLowerCase().equals("partiallinktext")) return By.partialLinkText(locatorValue); else if(locatorType.toLowerCase().equals("cssselector")||locatorType.toLowerCase().equals("css")) return By.cssSelector(locatorValue); else if(locatorType.toLowerCase().equals("xpath")){ return By.xpath(locatorValue); } else { throw new Exception("The input locator type is not defined in the program: "+locatorType); } } }
Step 2: Analyze the web page to be tested, and store the positioning expression of the element to be positioned in the configuration file (here, ObjectMap.properties)
SohuMai.HomePage.username=xpath:.//*[@id='theme']/form/div[1]/div[1]/input SohuMai.HomePage.password=xpath:.//*[@id='theme']/form/div[2]/div[1]/input SohuMai.homePage.submitButton=xpath:.//*[@id='theme']/form/div[5]/input
Step 3: Write Test Classes
package cn.om.webdriverapi; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterMethod; public class TestSohuMailLoginByObjectMap { private WebDriver driver; private ObjectMap objectmap; String url; @Test public void testSohuMailLoginByObjectMap() { driver.get(url); // The default timeout for each fetch element is 10 seconds. If it is not set, it will time out before the acquisition is completed. show failure driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); try { objectmap = new ObjectMap("F:\\workspace\\WebDriver API\\ObjectMap.properties"); WebElement username = driver.findElement(objectmap.getLocator("SohuMai.HomePage.username")); System.out.println("-------------------------------------------------"); WebElement password = driver.findElement(objectmap.getLocator("SohuMai.HomePage.password")); System.out.println("-------------------------------------------------"); WebElement submitButton = driver.findElement(objectmap.getLocator("SohuMai.homePage.submitButton")); System.out.println("-------------------------------------------------"); username.sendKeys("fosterwu"); password.sendKeys("1111"); submitButton.click(); } catch (Exception e) { e.printStackTrace (); } } @BeforeMethod public void beforeMethod() { url = "http://mail.sohu.com"; System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); } @AfterMethod public void afterMethod() { driver.quit(); } }