启动第一个浏览器

时间:2015-08-01 13:49:27

标签: java selenium-webdriver testng selenium-ide selenium-chromedriver

我正在尝试在不同的浏览器上运行一些基本测试,但测试在启动第一个浏览器(在这种情况下是Firefox)后失败了一次,并且无法执行Chrome和IE浏览器。

使用TestNG注释的Java代码 -

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("http://stackoverflow.com/questions/31747315/getting-nullpointerexception-when-trying-to-generate-test-reports-using-extentre/31752389#31752389");
    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 : http://stackoverflow.com/questions/31747315/getting-nullpointerexception-when-trying-to-generate-test-reports-using-extentre
 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 -->

输出 - 运行更改的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 : http://stackoverflow.com/questions/31747315/getting-nullpointerexception-when-trying-to-generate-test-reports-using-extentre
 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
 ===============================================

1 个答案:

答案 0 :(得分:0)

参数标签应该在课外。

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