gpt4 book ai didi

javascript - selenium webdriver 不适用于 jasmine

转载 作者:行者123 更新时间:2023-12-02 14:03:19 24 4
gpt4 key购买 nike

我正在尝试使用 Jasmine 和 selenium webdriver 编写一个简单的 UI 测试。

但是,我无法让它发挥作用。以下是我使用的环境:

  • Node v6.6.0
  • jasmine@2.5.2(作为全局模块安装)
  • selenium-webdriver@3.0.0-beta-3(安装在node_modules中)
  • ChromeDriver 2.25.426935(位于/usr/local/bin 中)
  • 最新的 Google Chrome(版本 54.0.2840.71(64 位))
  • Mac OS X 10.11.6 (El Capitan)

代码非常简单( typescript ):

import * as webdriver from 'selenium-webdriver';

let by = webdriver.By;
let until = webdriver.until;

describe("my suite", () => {

beforeEach(() => {
})

afterEach(() => {
})

it("should work", () => {
console.log("==========>");
let driver = new webdriver.Builder()
.forBrowser("chrome")
.build();
driver.get("https://www.google.com")
.then(s => {
return driver.getTitle();
})
.then(title => { console.log(title) })
console.log("<==========");
})
});

生成的JS代码:

"use strict";
var webdriver = require('selenium-webdriver');
var by = webdriver.By;
var until = webdriver.until;
describe("my suite", function () {
beforeEach(function () {
});
afterEach(function () {
});
it("should work", function () {
console.log("==========>");
var driver = new webdriver.Builder()
.forBrowser("chrome")
.build();
driver.get("https://www.google.com")
.then(function (s) {
return driver.getTitle();
})
.then(function (title) { console.log(title); });
console.log("<==========");
});
});

相同的代码可以作为独立的 JS(带 Node )运行,没有任何问题:

"use strict";
var webdriver = require('selenium-webdriver');
var by = webdriver.By;
var until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser("chrome")
.build();
driver.get("https://www.google.com").then(function (s) {
return driver.getTitle();
})
.then(function (title) { console.log(title); });
driver.quit();

我不知道 jasmine 与 selenium 一起工作是否需要任何配置。请帮忙。

Moving the webdriver initialization to "beforeEach" does not help. If I can make the code work, I'll move it to "beforeEach" and quit it in "afterEach".

最佳答案

我明白了。由于 selenium-webdriver 是异步操作,因此我需要对 jasmine/mocha 使用异步风格。

import * as webdriver from 'selenium-webdriver';

let by = webdriver.By;
let until = webdriver.until;

describe("my suite", () => {

beforeEach(() => {
})

afterEach(() => {
})

it("should work", (done) => {
console.log("==========>");
let driver = new webdriver.Builder()
.forBrowser("chrome")
.build();
driver.get("https://www.google.com")
.then(s => {
return driver.getTitle();
})
.then(title => { console.log(title); done(); })
console.log("<==========");
})
});

关于javascript - selenium webdriver 不适用于 jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40233483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com