【问题标题】:Run step to open browser in cucumber / selenium test once before running all scenario examples then run step to close browser afterwards在运行所有场景示例之前运行一次以在黄瓜/硒测试中打开浏览器的步骤,然后运行步骤以关闭浏览器
【发布时间】:2022-02-01 16:28:07
【问题描述】:

对于措辞不当的问题,我们深表歉意。我是测试自动化和 selenium / cucumber BDD 的新手,我正在尝试创建一个演示项目,该项目使用 google 运行一些数学表达式并验证结果。我想使用场景大纲,所以有..

Scenario Outline: Enter a maths expression
    Given User enters the maths expression <expression>
    When the user clicks the Search button
    Then The result of <expectedResult> is returned  
Examples: 
    | expression | expectedResult |
    | 4+4 | 8 | 
    | 5^3 | 125 |  

我有我的步骤来打开 chrome 浏览器并在 @Before 方法中导航到谷歌,比如 ..

@Before
public void setup() throws Throwable {
    System.setProperty("webdriver.chrome.driver","C:\xxx\\chromedriver.exe");
    this.driver = new ChromeDriver();
    this.driver.manage().window().maximize();
    this.driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
    this.driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
    driver.get("https://google.com");
    Thread.sleep(2000);
    //agrees to the terms and conditions
    driver.findElement(By.xpath("//*[@id=\"L2AGLb\"]/div")).click();
}

当我运行我的功能时,它可以工作,但是对于场景大纲中的每个示例,它都会运行之前的步骤(这似乎是在线阅读的预期行为),即浏览器会为每个示例打开和关闭。

理想情况下,我希望浏览器在开始时打开一次,运行所有示例,然后在结束时关闭。是否有适当的方法注释可用于实现此目的。我试图在网上找到答案,但没有任何运气。一篇文章提到了使用@BeforeSuite,但我只是在尝试使用它时出错。我也尝试使用 @Background 注释,但这与它似乎适用于每个场景示例一样。

任何帮助将不胜感激。

【问题讨论】:

  • 我的回答清楚吗?这就是你要找的东西吗?

标签: java selenium cucumber


【解决方案1】:

你的断言是正确的。 Cucumber Hook @Before 将在每个场景的第一步之前执行带有此注释标记的方法 [参见:Cucumber Reference]。显然,如果您不想在每个场景之前和之后打开和关闭浏览器,那么您无法在此处添加该代码。不幸的是,Cucumber 没有像 JUnit 或 TestNG 那样运行“Before Suite”的钩子。因此,您不能像在那些单元测试平台上那样先或后运行某些东西。

我使用的一个技巧是初始化驱动程序,然后将其作为全局变量存储在实用程序类中。这样,我可以在整个测试会话中重用同一个实例。此外,您可以创建一个 init 场景并运行您的场景。

@StartBrowser
Scenario: Start browser session
  When I initialize driver
  Then open browser

在代码中...

public class BrowserInitStepDefinitions {
    @When("I initialize driver")
    public void initializeDriver() {
        System.setProperty("webdriver.chrome.driver","C:\xxx\\chromedriver.exe");
        WebDriverUtils.setDriver(new ChromeDriver());
    }

    @Then ("open browser")
    public void openBrowser() {
        WebDriver driver = WebDriverUtils.getDriver();
        driver.get("https://www.yourwebsite.com");
    }

    @Then ("close browser")
    public void closeBrowser() {
        WebDriver driver = WebDriverUtils.getDriver();
        driver.quit();
    }
}
public class WebDriverUtils {
    private static WebDriver driver;
    public static void setDriver(WebDriver webDdriver) {
        if (driver == null) {
            driver = webDdriver;
        }
    }
    public static WebDriver getDriver() {
        if (driver == null) {
            throw new AssertionError("Driver is null. Initialize driver before calling this method.");
        }
        return driver;
    }
}

最后,当您调用您的场景时,请记住首先包含这些场景。

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "html:target/cucumber-report.html"},
        features= "features",
        glue= {"whatever"},
        tags= "@StartBrowser or @MyTest1 or @MyTest2"
        )
public class TestRunner {
    // code omitted
}

您可以(并且很可能应该)创建一个场景来关闭您的浏览器并回收您在执行测试期间可能使用过的任何资源。

【讨论】:

  • 感谢 hfontanez。我试图将您的代码合并到我的 StepDef 类中,但在 OpenBrowser 方法中出现了一些错误,例如“类型不匹配:无法从 void 转换为 WebDriver”。我猜那是因为 WebDriverUtils 中的 getDriver 方法被声明为 void?我将getDriver的类型从void更改为WebDrive,但还有其他错误,例如''方法setDriver不能被声明为静态;静态方法只能在静态或顶级类型中声明”。对不起,我的 java 知识不好,所以不知道如何解决这些问题。但明天会仔细看看。
  • Sorry..我这里直接写了代码。我修复了WebDriverUtils
  • 太棒了。非常感谢。明天会看。已经关闭 Eclipse :-)
  • 嗨。我做了你建议的 chgs,但仍然出现错误。BrowserInitStepDefinitions cls 最初没有错误,但 WebDriverUtils 对私有 webDriver 变量有“字段驱动程序不能在非静态内部类型中声明为静态,除非用常量表达式初始化” .针对我得到的 2 个 mthds 。“方法 .. 不能声明为静态的;静态方法只能在静态或顶级类型中声明”。我可以通过删除静态 mdifier 来消除错误,但随后我在 BrowserInitStepDefinitions cls 上看到错误。 "无法静态引用 ..
  • 您将 WebDriverUtils 创建为非静态内部类。它应该是一个外部(正常)类。我什至把自己的一套```里面的代码分开了。
猜你喜欢
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多