简体   繁体   English

断言硒webdriver节点js

[英]assertion selenium webdriver node js

I am trying to run assertion for testing with selenium webdriver through node js but it says undefined, I get the page title which is URL of the page then assert it, looks like I have to import sth for assertion, please help, also please tell me if selenium works fine with node js here is my code: 我正在尝试通过节点js运行使用硒Webdriver进行测试的断言,但是它说未定义,我得到的页面标题是页面的URL,然后断言它,看起来我必须导入某事物进行断言,请帮助,也请告诉如果硒与节点js正常工作,这是我的代码:

var webdriver = require('selenium-webdriver'),
//var test = require('selenium-webdriver/testing'),
nodeThen = require('node-then');
var assert = require('assert');
//var jsdom = require("jsdom");
//var document = require('jquery');
var xpath = require('xpath');
//var driver = new webdriver.Builder().
 // withCapabilities(webdriver.Capabilities.chrome()).
 //build();

function createDriver() {
    var driver = new webdriver.Builder()
        .usingServer('link')
        .withCapabilities(webdriver.Capabilities.chrome())
        .build();
    driver.manage().timeouts().setScriptTimeout(10000);
    return driver;
}

var driver = createDriver();
var By = webdriver.By;


driver.get("URL")
    .then(function(){
        driver.sleep(10000);
        var element=driver.findElement(By.id("get-started"));
        element.click();

    })
    .then(function(){`enter code here`
        return driver.getTitle();
    })
    .then(function(title) {
         //console.log(title);
         //driver.manage().timeouts().setScriptTimeout(50000);
        if (title == ('URL')) {
       console.log("pass");
        }
//

I was searching for the same issue and I found this snippet which is working for me 我在搜索相同的问题,但发现此片段对我有用

driver.findElement(By.id('elementId'))
      .getText().then(textValue => {
        assert.equal('tested string', textValue);
      });

I found it in the examples files of selenium-webdriver's github repo 我在selenium-webdriver的github存储库的示例文件中找到了它

Did you install asserts? 您是否安装了断言? The command would be npm install asserts . 该命令将为npm install asserts Also, you need var Asserts = require('asserts'); 另外,您需要var Asserts = require('asserts');

This is the example you are looking for 这是您要查找的示例

// Require chai.js expect module for assertions
const chai = require('chai');
const expect = require('chai').expect;

// Application Server
const serverUri = '0.0.0.0:3000';

// Official selenium webdriver testing setup
const webdriver = require('selenium-webdriver');

describe('basic test', function () {
    let driver;
    before(() => {
        // Start of test use this
        driver = new webdriver.Builder().
        withCapabilities(webdriver.Capabilities.chrome()).
        build();
        console.log("Selenium Webdriver Chrome Started");
    });

    after(function(){
        // End of test use this.
        driver.quit();
    });

    it('should be on correct page', function (done) {
        this.timeout(10000);
        driver.get(serverUri);
        driver.getTitle().then(function(title) {
            expect(title).to.equal('Some String Here');
            done();
            console.log("Selenium Webdriver Chrome Shutdown");
        })
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM