Java 如何在SeleniumWebDriver中进行参数化?

Java 如何在SeleniumWebDriver中进行参数化?,java,xml,junit,webdriver,testng,Java,Xml,Junit,Webdriver,Testng,如何在Selenium 2(WebDriver)中进行参数化?我将Eclipse与maven插件一起使用,以前没有使用SeleniumWebDriver的经验。当我用谷歌搜索它时,一切都显示了testNG和JUnit。有什么方法可以参数化Webdriver吗?我假设您想将一些参数传递给Webdriver。这可以通过两种方式实现: package pac1; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;

如何在Selenium 2(WebDriver)中进行参数化?我将Eclipse与maven插件一起使用,以前没有使用SeleniumWebDriver的经验。当我用谷歌搜索它时,一切都显示了testNG和JUnit。有什么方法可以参数化Webdriver吗?

我假设您想将一些参数传递给Webdriver。这可以通过两种方式实现:

 package pac1;
 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Wait;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
 import org.w3c.dom.*;

public class test extends sut  {
static WebDriver driver;
static Wait<WebDriver> wait;
  public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
{
    driver = driverArg;
    wait = waitArg;
   // Run all the methods and return false if any fails
    return (test1() );
                  } 
   private static boolean test1()
   {



  driver.get("https://accounts.google.com");

    try {
        File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
        // Prepare XML
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(file);
        document.getDocumentElement().normalize();
        NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
        for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
    {
       String client = "The username or password you entered is incorrect. ?";
        Element emailNodeElement = (Element)emailNodeElementList.item(j);
        NodeList details = emailNodeElement.getChildNodes();
        String emailAddress=((Node) details.item(0)).getNodeValue();
        System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
        WebElement element = driver.findElement(By.cssSelector("body"));
            boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
            boolean feedbackVisible = element.isDisplayed();


    WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
    e1.sendKeys(emailAddress);//sending keys to the server
    WebElement e3 = driver.findElement(By.id("signIn"));
    e3.click();


    if(feedBack==true){
        System.out.println(client+ "is present");
        if(feedbackVisible==true){
            System.out.println(client+ "is visible");
        }
        else{
            System.out.println(client+ "is not visible");
        }

    }
    else{
        System.out.println(client+ "is not present");

    }
    }
  }
 catch (Exception e) {e.printStackTrace();}

  return true;
  }}
  • 生成扩展Webdriver的类,并使其构造函数具有需要传递的参数。但是,这是一种困难的方法,因为您必须从webdriver实现/覆盖所有(需要的)功能:

    public class MyWebdriver extends Webdriver{
          private String theParameter;
    
         public MyWebdriver(String parameter){
           //... initialize the Webdriver
    
           //store the parameter
           theParameter = parameter
    }
    
     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
  • 制作您自己的包装器,其中将包含WebDriver之外的实例。这很容易。例如:在我自己的测试中,我需要告诉Webdriver我正在测试哪个环境。因此,我为环境创建了自己的类:

    public class Environment{
      private String baseUrl;
      public enum NameOfEnvironment {DEV, ACC}
      private NameOfEnvironment environment;
    
      public Environment(NameOfEnvironment envName){
         environment = envName;
      }
    
      public String getBaseUrl(){
          switch (environment){
             case DEV: baseUrl = "https://10.10.11.12:9080/test/";
                       break;
             case ACC: baseUrl = "https://acceptance.our-official-site.com";
                       break;
          }
        return baseUrl;
      }
     }
    
     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
  • 然后我有了自己的WebDriver包装器,在这里我像这样初始化它:

    public class TestUI{
          private Webdriver driver;
          private Environment env;
    
       public TestUI(Environment e){
           this.env = e;
           driver = new FirefoxDriver;
           driver.get(env.getBaseUrl());
       }
    }
    
     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
    在测试中:

     public class TestCases{
    
       public static final Environment USED_ENVIRONMENT = new Environment(Environment.NameOfEnvironment.ACC);
    
     @Test
     public void testSomething(){
        testUI test = new testUI(USED_ENVIRONMENT);
        //.. further steps
     }
     }
    
     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    

    我的建议是尝试使用一个测试框架(TestNG或Junit),它提供了比参数化更多的特性。当您的测试代码增长时,在开始时设置框架的一点努力可能会节省大量的努力

    public void property(){
        try {
    
            File file = new File("login.properties");
            FileInputStream fileInput = new FileInputStream(file);
            Properties properties = new Properties();
            properties.load(fileInput);
            fileInput.close();
    
            Enumeration enuKeys = properties.keys();
            while (enuKeys.hasMoreElements()) {
                String key = (String) enuKeys.nextElement();
                String value = properties.getProperty(key);
                driver.findElement(By.id(key)).sendKeys(value);
                System.out.println(key + ": " + value);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
    要从属性文件传递值,请使用property();在主课上。执行

    @Parameters({ "first-name" })
    @Test
    public void testSingleString(String firstName) {
      System.out.println("Invoked testString " + firstName);
      assert "Cedric".equals(firstName);
    }
    
     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
    在这段代码中,我们指定Java方法的参数firstName应该接收名为firstName的XML参数的值。此XML参数在testng.XML中定义:

     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
    有关更多详细信息,请访问以下网站:

    这里我提供了一个可能有用的测试用例

     package pac1;
     import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Wait;
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
     import org.w3c.dom.*;
    
    public class test extends sut  {
    static WebDriver driver;
    static Wait<WebDriver> wait;
      public static boolean run(WebDriver driverArg, Wait<WebDriver> waitArg)
    {
        driver = driverArg;
        wait = waitArg;
       // Run all the methods and return false if any fails
        return (test1() );
                      } 
       private static boolean test1()
       {
    
    
    
      driver.get("https://accounts.google.com");
    
        try {
            File file = new File("emaildata.xml"); //file location should be specified correctly put your xml in the same folder of the source code.
            // Prepare XML
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            NodeList emailNodeElementList = document.getElementsByTagName("test");//test is the name of the child tag 
            for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
        {
           String client = "The username or password you entered is incorrect. ?";
            Element emailNodeElement = (Element)emailNodeElementList.item(j);
            NodeList details = emailNodeElement.getChildNodes();
            String emailAddress=((Node) details.item(0)).getNodeValue();
            System.out.println("email :" + emailAddress);//it just prints which email is going to be parsed
            WebElement element = driver.findElement(By.cssSelector("body"));
                boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
                boolean feedbackVisible = element.isDisplayed();
    
    
        WebElement e1 = driver.findElement(By.id("Email"));//getting the location from the web
        e1.sendKeys(emailAddress);//sending keys to the server
        WebElement e3 = driver.findElement(By.id("signIn"));
        e3.click();
    
    
        if(feedBack==true){
            System.out.println(client+ "is present");
            if(feedbackVisible==true){
                System.out.println(client+ "is visible");
            }
            else{
                System.out.println(client+ "is not visible");
            }
    
        }
        else{
            System.out.println(client+ "is not present");
    
        }
        }
      }
     catch (Exception e) {e.printStackTrace();}
    
      return true;
      }}
    
    pac1包;
    导入org.openqa.selenium.By;
    导入org.openqa.selenium.WebDriver;
    导入org.openqa.selenium.WebElement;
    导入org.openqa.selenium.support.ui.Wait;
    导入java.io.File;
    导入javax.xml.parsers.DocumentBuilder;
    导入javax.xml.parsers.DocumentBuilderFactory;
    导入org.w3c.dom.*;
    公共类测试扩展了sut{
    静态网络驱动程序;
    静态等待;
    公共静态布尔运行(WebDriver driverArg、Wait waitArg)
    {
    司机=司机;
    wait=waitArg;
    //运行所有方法,如果任何方法失败,则返回false
    返回(test1());
    } 
    私有静态布尔test1()
    {
    驱动程序。获取(“https://accounts.google.com");
    试一试{
    File File=new File(“emaildata.xml”);//应正确指定文件位置。请将xml放入源代码的同一文件夹中。
    //准备XML
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document=db.parse(文件);
    document.getDocumentElement().normalize();
    NodeList emailNodeElementList=document.getElementsByTagName(“test”);//test是子标记的名称
    
    对于(int j=0;jj),您可以使用JUnit或TestNG作为测试框架。您想使用什么?我更喜欢TestNG。如果您使用TestNG,您需要创建一个XML文件,例如TestNG.XML,在这个文件中,您可以使用TestNG的@Parameters注释指定要参数化的数据(这在上面的代码字符串Username=read.readData(“Sheet1”)片段中已经提到过,1,2);driver.findElement(By.id(“ap_email”)).sendKeys(Username);driver.findElement(By.id(“continue”)).click();String Password=read.readData(“Sheet1”,1,3);driver.findElement(By.id(“ap_Password”).sendKeys(Password);感谢您的贡献。请花点时间访问。您应该解释一下为什么您认为这是一个正确的答案