启动第一个浏览器后,TestNG 跨浏览器测试失败

TestNG cross browser test fails after launcing first browser

我正在尝试 运行 在不同的浏览器上进行一些基本测试,但在启动第一个浏览器(在本例中为 Firefox)后测试失败一次并且无法执行 Chrome 和 IE浏览器。

Java 使用 TestNG 注释的代码 -

package com.Selenium_Practice;

import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;

public class CrossBrowserTestingUsingTestNG {

WebDriver driver;
ExtentReports report = ExtentReports.get(CrossBrowserTestingUsingTestNG.class);

@SuppressWarnings("deprecation")
@Test
@Parameters("browser")
public void CrossBrowserCodeBrowser(String browserName){

    report.init("E:\Report\CrossBrowserTestingUsingTestNG.html", true);
    report.startTest("Starting the test for CrossBrowserTestingUsingTestNG class");


    if(browserName.equalsIgnoreCase("firefox")){
      driver = new FirefoxDriver();
      report.log(LogStatus.INFO, "Firefox browser is up and running");
    }//if

    else if(browserName.equalsIgnoreCase("chrome")){
      System.setProperty("webDriver.chrome.driver", "E:\Driver\chromedriver.exe");
      driver = new ChromeDriver();  
      report.log(LogStatus.INFO, "Chrome browser is up and runnning");
    }//else if

    else if(browserName.equalsIgnoreCase("ie")){
      System.setProperty("webDriver.chrome.driver", "E:\Driver\IEDriverServer.exe");
      driver = new InternetExplorerDriver();
      report.log(LogStatus.INFO, "IE is up and running");
    }//else if

    driver.manage().window().maximize();
    driver.get("");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElement(By.xpath(".//*[@id='question-header']/h1/a")).getAttribute("href");
    driver.findElement(By.xpath(".//*[@id='question-header']/h1/a")).getTagName();
    System.out.println("Link : "+driver.findElement(By.xpath(".//*[@id='question-header']/h1/a")).getAttribute("href"));
    System.out.println("Question : "+driver.findElement(By.xpath(".//*[@id='question-header']/h1/a")).getTagName());
    Assert.assertEquals("a", driver.findElement(By.xpath(".//*[@id='question-header']/h1/a")).getTagName());
    report.log(LogStatus.PASS, "Both are equal");
    report.endTest();
    driver.quit();
 }//CrossBrowserCodeBrowser

}//CrossBrowserTestingUsingTestNG

测试服-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="CrossBrowserTesting" parallel="none">

 <test name="FirefoxTestSuit">    
  <classes>
    <parameter name="browser" value="firefox"/> 
   <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>  
 </classes>  
</test> <!-- CrossBrowser -->

<test name="ChromeTestSuit">  
  <classes>
   <parameter name="browser" value="chrome"/> 
    <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>  
  </classes>  
</test> <!-- CrossBrowser -->

<test name="IETestSuit">
 <classes>
   <parameter name="browser" value="ie"/>
    <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>
  </classes>
 </test> <!-- CrossBrowser -->

</suite> <!-- Suite -->

使用 TestNG 运行 处理 XML 文件后,我得到以下结果 -

[TestNG] Running:
 D:\Java Programs and files\com.Selenium_Practice\CrossBrowsertestng.xml

 log4j:WARN No appenders could be found for logger  (org.apache.http.client.protocol.RequestAddCookies).
 log4j:WARN Please initialize the log4j system properly.
 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig  for more info.
 Link : 
 Question : a
 Aug 01, 2015 6:28:10 AM  org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
 INFO: Command failed to close cleanly. Destroying forcefully (v2).   org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@1503f6b

 ===============================================
 CrossBrowserTesting
 Total tests run: 3, Failures: 2, Skips: 0
 ===============================================

已更新 XML 文件 - 在下面的 XML 文件中,我将参数放在 类 之外。

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 <suite name="CrossBrowserTesting" parallel="none">

 <test name="FirefoxTestSuit">  
  <parameter name="browser" value="firefox"/>  
   <classes>
    <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>  
   </classes>  
  </test> <!-- CrossBrowser -->

  <test name="ChromeTestSuit">  
   <parameter name="browser" value="chrome"/> 
    <classes>
     <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>  
    </classes>  
   </test> <!-- CrossBrowser -->

<test name="IETestSuit">
 <parameter name="browser" value="ie"/>
  <classes>
    <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>
  </classes>
 </test> <!-- CrossBrowser -->  
 </suite> <!-- Suite -->

输出- 在 运行ning 更改后的 xmfile 之后,我得到了相同的结果。

 [TestNG] Running:
  D:\Java Programs and files\com.Selenium_Practice\CrossBrowsertestng.xml

 log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
 log4j:WARN Please initialize the log4j system properly.
 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
 Link : 
 Question : a
 Aug 02, 2015 12:29:54 PM  org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
 INFO: Command failed to close cleanly. Destroying forcefully (v2).  org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@1be3395

 ===============================================
 CrossBrowserTesting
 Total tests run: 3, Failures: 2, Skips: 0
 ===============================================

参数标签应该在 类 之外。

<test name="FirefoxTestSuit">  
<parameter name="browser" value="firefox"/>   
  <classes>
     <class name="com.Selenium_Practice.CrossBrowserTestingUsingTestNG"/>  
 </classes>  
</test> <!-- CrossBrowser -->