【问题标题】:Unable to launch the browser using testng using beforeTest annotation无法使用 testng 使用 beforeTest 注释启动浏览器
【发布时间】:2018-03-20 11:47:52
【问题描述】:
 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:

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

【问题讨论】:

  • 这也是我的 testng.xml 文件:ttp://testng.org/testng-1.0.dtd" rel="nofollow" target="_blank">testng.org/testng-1.0.dtd" >
  • 你能用xml编辑你的问题吗?它会更容易阅读。谢谢
  • 已在问题中添加。
  • 这个仓库中的代码示例可能对你有帮助github.com/igniteram/testng-cucumber

标签: java selenium webdriver cucumber testng


【解决方案1】:

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

所以你有两个选择:

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

【讨论】:

    猜你喜欢
    • 2020-04-23
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多