【问题标题】:selenium-webdriver won't execute next step after opening browserselenium-webdriver 在打开浏览器后不会执行下一步
【发布时间】:2018-06-20 08:53:55
【问题描述】:

我一直在尝试使用cucumber-jsselenium-webdriver 来自动化我们的网络测试。我写了一个简单的网络导航示例,但我总是得到空白页面,跑步者停止做任何事情。这是代码片段:

// my_project/features/step_definitions/SomeTest.js

const { Given, When, Then } = require('cucumber')
const { assert, expect } = require('chai')
const webdriver = require('selenium-webdriver')

var browser = new webdriver.Builder()
.forBrowser('chrome')
.build();

Given("I'm on landing page", function() {
    browser.get('https://www.google.com')
});

这是我的 SomeTest.feature:

// my_project/features/SomeTest.feature

    Feature: Some Test

    As a user I want to search a keyword on Google

    @first
    Scenario: Search a word
    Given I'm on landing page
    When I typed in "test"
    Then I should get redirected search result page

在我使用./node_modules/.bin/cucumber-js 运行测试之后 我得到的总是在 chrome 或 firefox 上的空白页。

有人遇到同样的问题吗?知道如何解决或至少调试此问题吗?

附:我正在使用在 64 位 ubuntu 14.04 上运行的 Chrome 65chromedriver 2.40.565383Firefox 56geckodriver 0.21.0

【问题讨论】:

  • "I'm on landing page" 与什么有关? Afaik 应该有一些与此相关的代码。如果不满足该条件,内部代码将不会执行
  • 是的。它在小黄瓜功能文件上,我已经定义了。网络驱动程序似乎只生成浏览器而不继续执行代码
  • 我想改进我的答案。您也可以发布这些文件吗?编辑问题并将它们添加到那里
  • 我明白了,我会更新的。

标签: javascript selenium testing automated-tests cucumberjs


【解决方案1】:

你需要的是:

Given("I'm on landing page", function() {
    return browser.get('https://www.google.com')
});

或者

Given("I'm on landing page", function(callback) {
    browser.get('https://www.google.com');
    callback();
});

返回和回调将向函数(以及黄瓜)表示该步骤已完成执行。

在某些情况下,您可能希望等待内部的所有内容按顺序执行,这就是 asyncawait 出现的地方(在 Node 10.3.0+ 上可用):

Given("I'm on landing page", async function() {
    return await browser.get('https://www.google.com');
});

【讨论】:

    【解决方案2】:

    正如example of cucumber-js中所见,你需要:

    • 功能文件
    • 步骤定义
    • 使用步骤定义的代码

    在你解决这个问题之前,这段代码确实不会执行:

    Given("I'm on landing page", function() {
        browser.get('https://www.google.com')
        browser.quit()
    });
    

    【讨论】:

    • 不,我已经得到了所有这些,它仍然会发生。谢谢你的回答:)
    • Kyle 想说的是对这个答案投反对票,对他投赞成票
    【解决方案3】:

    其实我只是想通了,

    我需要在函数参数中添加“回调”,如下所示:

    Given("I'm on landing page", function() {
        browser.get('https://www.google.com')
        browser.quit()
    });
    

    对此,

    Given("I'm on landing page", function(callback) {
        browser.get('https://www.google.com')
        browser.quit()
    });
    

    【讨论】:

    • 这解决了空白页的问题,但是因此出现了另一个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2012-04-08
    • 2018-03-01
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多