2017-10-09 66 views
0
I have used cucumber BDD for my testcases with testng as the test engine. I have added the necessary jars in my POM.xml as mentioned below. I am using selenium 2.53 and using Firefox browser. 

<!-- language: lang-xml --> 

    <dependency> 
     <groupId>org.testng</groupId> 
     <artifactId>testng</artifactId> 
     <version>6.11</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng --> 
    <dependency> 
     <groupId>info.cukes</groupId> 
     <artifactId>cucumber-testng</artifactId> 
     <version>1.2.5</version> 
    </dependency>   

In runner class I have inherited the `AbstractTestNGCucumberTests` as below :  

     package com.cucumber.bhsibase.runner;   
     import org.junit.runner.RunWith;   
     import cucumber.api.CucumberOptions; 
     import cucumber.api.junit.Cucumber; 
     import cucumber.api.testng.AbstractTestNGCucumberTests;   
     @RunWith(Cucumber.class) 
     @CucumberOptions(features = "src/test/resources", glue = "com.cucumber.bhsibase.party.tests") 
     public class TestRunner extends AbstractTestNGCucumberTests {  
     } 

I want to launch the browser with the url, so I have added that part of the code in a method with `@BeforeTest` annotation, but nothing happens. Tried the same using `@BeforeCLass` too, but it's the same. 

Please find the code below: 

     public class PartyBase { 

      // public static WebDriver driver; 

      ReadXML readxmlobj = new ReadXML(); 

      final static Logger logger = Logger.getLogger(PartyBase.class); 


      ConfigReader reader = new ConfigReader(); 
      public static WebDriver driver; 

     @BeforeTest 
      public void geturl() throws Exception { 
       logger.info("Entering the execute message"); 

       driver = new FirefoxDriver(); 
       logger.info("Mozzilla Browser opens"); 
       driver.get(reader.readurls("ExpressURL")); 
       logger.info("Navigate to express"); 
       driver.manage().window().maximize(); 
       driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
      } 

Please let me know how to proceed ahead with this. The code runs fine only if the method is called explicitly in the given step definition of each scenarios.  
This issue is same if I use `@before` annotation of Junit as well. 

我的testng.xml:無法用TestNG使用beforeTest註解來啓動瀏覽器

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="Party" parallel="none"> 
<test name = "Base Party Validation"> 
<classes> 
<class name ="com.cucumber.bhsibase.runner.TestRunner"></class> 
</classes> 
</test> 
</suite> 

我已經加入只是我的TestRunner類。我的印象是,無論testng.xml中添加了哪個類,beforetest註釋都將在其他方法之前運行。無法繼續前進。我是否需要在testng.xml中添加任何其他類。

+0

此外,這是我的testng.xml文件: <!DOCTYPE套件系統 「http://testng.org/testng-1.0.dtd」> <套件名稱= 「黨」 平行= 「無」> <測試名稱= 「基本方認證」> <類名= 「com.cucumber.bhsibase.runner.TestRunner」> user3548850

+0

能否請您編輯與XML你的問題?它會更容易閱讀。感謝 – HaC

+0

已添加在問題中。 – user3548850

回答

0

您的套件xml僅包含對TestRunner類的引用,但它看起來像您的註釋配置方法駐留在不包含在套件xml文件中的不同類(您的情況爲PartyBase)中。

所以,你有兩個選擇:

  1. 可以包括PartyBase也是您的套房xml文件(或)
  2. 移動這些標註您的TestRunner類本身。
+0

第二個選項可行。謝謝 !! – user3548850

+0

如果有幫助,你可以接受我的答案嗎? –

相關問題