2

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:

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");
        }
//

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

Did you install asserts? The command would be npm install asserts. Also, you need var Asserts = require('asserts');

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.