使用TestNG + WebDriver XML问题进行并行测试?

时间:2016-01-18 13:52:12

标签: java xml selenium selenium-webdriver testng

尝试使用Selenium webdriver + testNG运行并行测试时遇到问题。我认为我的问题在于XML,但我不确定这是否是问题。以下是错误消息,我的代码是我出错了?

FAILED CONFIGURATION: @BeforeClass beforeTest
org.testng.TestNGException: 
Parameter 'browser' is required by @Configuration on method beforeTest but has not been marked @Optional or defined
in C:\Users\robertr\AppData\Local\Temp\testng-eclipse--933208925\testng-customsuite.xml

SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED: login("jane20@servicecharges.co.uk", "password1", "smith")
SKIPPED: login("rob23@orchard.co.uk", "password1", "smith")
SKIPPED: login("jeff23@hotmail.com", "password1", "smith")
SKIPPED: login("rob23@orchard.co.uk", "password1", "smith")

测试代码:

public class ParallelLogin {

    private static Logger logger1 = LogManager.getLogger("Logger1");

    WebDriver driver;

    @Parameters({"browser"})

    @BeforeClass
      public void beforeTest(String browser) {
         // If the browser is Fire fox, then do this
         DOMConfigurator.configure("log4j.xml");

          if(browser.equalsIgnoreCase("firefox")) {

              driver = new FirefoxDriver();

          // If browser is IE, then do this   

          }else if (browser.equalsIgnoreCase("ie")) { 

              // Here I am setting up the path for my IEDriver

              System.setProperty("webdriver.ie.driver", "http://localhost/2010A15/");

              driver = new InternetExplorerDriver();

          } 

          driver.get("http://localhost/2010A15/");
      }

  @Test(dataProvider = "Authentication")
  public void login(String sUsername, String sPassword, String sMemorableWord) throws InterruptedException {

    //Find Login Element
      driver.findElement(By.id("WEB_LoginButton")).click();

      logger1.info("Login Button Clicked");
      //Find User name Element

      driver.findElement(By.cssSelector("#dijit_form_ValidationTextBox_4")).sendKeys(sUsername);

      logger1.info("Username Entered");

      //Find Password Element

      driver.findElement(By.cssSelector("#dijit_form_ValidationTextBox_5")).sendKeys(sPassword);

      logger1.info("Password Entered");

      //Find Memorable word Element

      driver.findElement(By.cssSelector("#dijit_form_ValidationTextBox_6")).sendKeys(sMemorableWord);

      logger1.info("MemorableWord Entered");

      Reporter.log("All Login Details Entered  | "); //Main Event

      WebElement  login = driver.findElement(By.id("dijit_form_Button_1_label"));

      if(login.isDisplayed()){
          login.click();
          Reporter.log("Submit Button Clicked  |");
      }

        // Now check again for the login button. 
        // If login button is displayed it means that the form submission
        // is not successful and you have returned to the same page.
        // We are using a try catch block here because if the element is not found
        // if form submission is successful NoSuchElementException would be thrown.
        try {
            // Element has to be found again else StaleElementReferenceException
            // might be thrown because of page reload.
          login = driver.findElement(By.id("dijit_form_Button_1_label"));
            // if login button is found again, this means form submission is not successful
            Reporter.log("Login Failed  | ");
            logger1.info("Login Failed");
        } catch (Exception e) {
            // Login button is not found. Form is submitted
           Reporter.log("Login Form Submitted  | ");
           logger1.info("Submit Button Clicked and successfully logged in  |");
        }
          //Main Event is logged If Passed


          //Main Event Log Fail  

         WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));

         if(logout.isDisplayed()){
             logout.click();
             Reporter.log("Logout Button Clicked  |");
}

         try{

             logout = driver.findElement(By.id("dijit_form_Button_0_label"));

             Reporter.log("Logout Failed  |");


         } catch (Exception e) {

             Reporter.log("Logout Submitted  |");
             logger1.info("Logout Submitted  |");
         }
    }



      //Check Reporter Results as these will be correct. Console Results will show if code has been run and passed.





  @DataProvider(name = "Authentication")
  public static Object[][] credentials() {
    return new Object[][] { { "jane20@servicecharges.co.uk", "password1", "smith" }, { "rob23@orchard.co.uk", "password1", "smith" }, { "jeff23@hotmail.com", "password1", "smith" }, { "rob23@orchard.co.uk", "password1", "smith" }}; 
     };






        @AfterMethod
          public void afterMethod() {

             driver.close();
          }
  }

XML代码:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="tests">

 <test name="FirefoxTest">

 <parameter name="browser" value="firefox" />

 <classes>

 <class name="utility1.ParallelLogin" />

 </classes>

 </test>

 <test name="IETest">

 <parameter name="browser" value="ie" />

 <classes>

 <class name="utility1.ParallelLogin" />

 </classes>

 </test>

1 个答案:

答案 0 :(得分:1)

如果要运行testng.xml

  1. 转到“运行配置”
  2. 创建新的testng配置
  3. 选择套件单选按钮并指定要使用的testng.xml文件
  4. enter image description here

    否则,如果您只想运行特定类,则可以为浏览器指定可选值

    @Parameters("browser")
    public void testNonExistentParameter(@Optional("ie") String browser)
    

    这样,当你在没有xml的情况下运行时,它将使用可选参数。

    有关详细信息,请查看文档 - http://testng.org/doc/documentation-main.html