0

Selenium 2(WebDriver)でパラメーター化を行う方法は?私はEclipseをMavenプラグインで使用していますが、SeleniumWebdriverの使用経験はありません。私がそれをグーグルで検索すると、testNGとJUnitについてすべてが表示されます。Webdriverをパラメータ化できる方法はありますか?

4

7 に答える 7

0

私は仮定をするつもりです-あなたはいくつかのパラメータをWebdriverに渡したいと思います。これは、次の2つの方法で実行できます。

  1. Webdriverを拡張するクラスを作成し、渡す必要のあるパラメーターを持つようにそのコンストラクターを作成します。ただし、これは難しい方法です。これは、Webドライバーからすべての(必要な)関数を実装/オーバーライドする必要があるためです。

    public class MyWebdriver extends Webdriver{
          private String theParameter;
    
         public MyWebdriver(String parameter){
           //... initialize the Webdriver
    
           //store the parameter
           theParameter = parameter
    }
    
  2. WebDriverからのインスタンスを含む独自のラッパーを作成します。それは簡単です(-ier)。例:私自身のテストでは、テストしている環境を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;
      }
     }
    

次に、独自のWebDriverラッパーがあり、次のように初期化します。

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
 }
 }
于 2012-05-01T20:50:59.670 に答える
0
@Parameters({ "first-name" })
@Test
public void testSingleString(String firstName) {
  System.out.println("Invoked testString " + firstName);
  assert "Cedric".equals(firstName);
}

このコードでは、JavaメソッドのパラメーターfirstNameがfirst-nameと呼ばれるXMLパラメーターの値を受け取る必要があることを指定します。このXMLパラメーターはtestng.xmlで定義されています:<-...->

詳細については、以下をご覧ください:http: //testng.org/doc/documentation-main.html#parameters

于 2012-08-07T09:16:50.013 に答える
0

ここで私は役に立つかもしれないテストケースを提供しています

 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;
  }}
于 2014-01-08T10:34:11.057 に答える
0

私の提案は、単なるパラメータ化よりも多くの機能を提供するテスト フレームワーク (TestNG または Junit) を使用することです。おそらく、最初にフレームワークをセットアップする際の少しの労力で、テスト コードが大きくなったときに多くの労力を節約できます。

于 2012-05-02T06:47:40.133 に答える
0
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();
    }
}

プロパティ ファイルから値を渡すには、property(); を使用します。メインクラスで。そして実行

于 2012-05-31T07:23:48.287 に答える