首页 > 解决方案 > selenium-webdriver 在打开浏览器后不会执行下一步

问题描述

我一直在尝试使用cucumber-jsand来自动化我们的 web 测试selenium-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 上的空白页。

这是我运行测试后得到的

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

PS 我正在使用Chrome 65, 和chromedriver 2.40.565383,Firefox 56geckodriver 0.21.0在 64 位 ubuntu 14.04 上运行

标签: javascriptseleniumtestingautomated-testscucumberjs

解决方案


你需要的是:

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');
});

推荐阅读