无法使用testT使用beforeTest注释启动浏览器

时间:2017-10-09 09:51:23

标签: java selenium webdriver cucumber testng

 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.  

My Testng.xml:

<!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中添加任何其他类。

1 个答案:

答案 0 :(得分:0)

您的套件xml仅包含对TestRunner类的引用,但看起来您的带注释的配置方法位于不同的类(在您的情况下为PartyBase)中,但未包含在套件中xml文件。

所以你有两个选择:

  1. 您也可以在套件xml文件(或)中包含PartyBase
  2. 将这些注释移到您的TestRunner类中。