English 中文(简体)
如何在Selenium Webdriver实现参数化?
原标题:How to do parameterization in Selenium Webdriver?

How to do parameterization in Selenium 2 (WebDriver)? I use Eclipse with maven plugins and I have no previous experience on Selenium Webdriver. When I google for it then every thing shows about testNG and JUnit. Is there any method through which we can parameterized the Webdriver?

问题回答

我将作假设——你想把一些参数传给网络的用户。 可以通过两种方式:

  1. 课堂 浏览器,使其施工商拥有你所需的参数。 然而,这是hard方式,因为你必须履行/超越网络用户的所有(必要的)职能:

    public class MyWebdriver extends Webdriver{
          private String theParameter;
    
         public MyWebdriver(String parameter){
           //... initialize the Webdriver
    
           //store the parameter
           theParameter = parameter
    }
    
  2. 自行总结,其中将载有从网络开车的事例。 页: 1 例如: 在我自己的测试中,我需要告诉我正在测试的环境的网络用户。 因此,我创建了我自己的环境班:

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

接着,我有自己的网络主持人总结,我开始这样做:

public class TestUI{
      private Webdriver driver;
      private Environment env;

   public TestUI(Environment e){
       this.env = e;
       driver = new FirefoxDriver;
       driver.get(env.getBaseUrl());
   }
}

在试验中:

 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
 }
 }

我的建议是努力利用试验框架(NG或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();
    }
}

从财产档案中提取价值使用财产;主要类别。 执行和执行

@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
  System.out.println("Invoked testString " + firstName);
  assert "Cedric".equals(firstName);
}

In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. This XML parameter is defined in testng.xml: <-- ... -->

For more details, please visit the following: http://testng.org/doc/documentation-main.html#parameters

这里,一是提供可能有助于检验的案件

 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;
  }}
/* You can pass parameters by creating two classes.
The first class, which you can call Main Class, will be public static void.
It will contain code such as:
*/
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Parameters id = new Parameters();   //this is referring to second class
driver.findElement(By.name("q")).sendKeys(id.x);
//****************************************************************
//this is the second class [separate page] - here you can declare int or string varibale as public [so you can share them with other class while defining them]  - 
//when you make this class - don t need to add public static void

public String x = "username"
//if you look at the first page, I m passing this string in the main script
public class ReadExcel {
       public String readData (String strSheetName, int strRowNo, int strCellNo) throws Exception {

              FileInputStream fis = new FileInputStream("D:\Selenium\TestData.xlsx");
              Workbook wb = WorkbookFactory.create(fis);
              Sheet sh = wb.getSheet(strSheetName);
              Row rw = sh.getRow(strRowNo);
              String val = rw.getCell(strCellNo).getStringCellValue();

              return val;

              }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签