javascript - selenium-webdriver 在打开浏览器后不会执行下一步
问题描述
我一直在尝试使用cucumber-js
and来自动化我们的 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 56
并geckodriver 0.21.0
在 64 位 ubuntu 14.04 上运行
解决方案
你需要的是:
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();
});
返回和回调将表示该步骤已完成执行的函数(以及黄瓜)。
在某些情况下,您可能希望等待内部的所有内容按顺序执行,这就是它async
的await
来源(在 Node 10.3.0+ 上可用):
Given("I'm on landing page", async function() {
return await browser.get('https://www.google.com');
});
推荐阅读
- python - 出现错误:无法在 django 中连接到“localhost”上的 MySQL 服务器([Errno 111] Connection denied)
- flask - 在单个 Flask routes.py 中替代全局变量(5 个函数中的 20 个变量)
- android - 无法安装应用程序:INSTALL_FAILED_OLDER_SDK,
- mediawiki - 使用摘录和缩略图查询随机文章的 MediaWiki API
- asp.net-mvc - 无法从 ASP.NET Core MVC 3.1 中的管理区域路由回
- java - 未能将 Spring Boot 应用程序部署到 WildFly
- java - 驱动路径不正确?我究竟做错了什么
- javascript - 无法读取 Express for POST 中的正文参数
- sql - 如何查找自某个日期以来不同记录的计数(例如:2018 年至今)?
- python - Python中的字典生成器