Search code examples
seleniumselenium-webdriverselenium-rcselenium-idesoda

how to generate dynamic asserts in nodejs soda


I have below code in nodejs for selenium using soda script.

If you see my script below, look for verifyData() There is row testing happening for fixed columns values. I want to generate these asserts dynamically, only row number will vary column will always same, how to achieve this. I can pass row number to this methods.

Second thing, if you see with assert I used function(), can we capture err/assertfail here?

var browser = soda.createClient({
.....
});

browser 
    .chain
    .session()  
    .setSpeed(speed)
    .setTimeout(2000)
    .open('/')
    .and(login('[email protected]', 'x1212GQsdtpS'))
    .and(verifyData())
    .end(function(err){
        console.log('error');

    });

function login(user, pass) {
  return function(browser) {
    browser
    .click('css=a#loginButton')
    .type('css=input.input-medium.email',user)
    .type('css=input.input.pwd',pass)
    .clickAndWait('css=a.btn.login')
    .assertTextPresent('Clients',function(){ console.log('logged in ok')})
  }
}


function verifyData() {
  return function(browser) {
    browser
    //Row 1 testing
    .assertText('css=div.keyYears table tbody tr:nth-child(1) td:nth-child(3)','some text',function(){ console.log('looks good')})
    .assertText('css=div.keyYears table tbody tr:nth-child(1) td:nth-child(5)','some text')
    .assertText('css=div.keyYears table tbody tr:nth-child(1) td:nth-child(6)','some text')
    .assertText('css=div.keyYears table tbody tr:nth-child(1) td:nth-child(7)','some text')
    //Row 2 testing
    .assertText('css=div.keyYears table tbody tr:nth-child(2) td:nth-child(3)','some text1',function(){ console.log('looks good')})
    .assertText('css=div.keyYears table tbody tr:nth-child(2) td:nth-child(5)','some text1')
    .assertText('css=div.keyYears table tbody tr:nth-child(2) td:nth-child(6)','some text1')
    .assertText('css=div.keyYears table tbody tr:nth-child(2) td:nth-child(7)','some text1')

  }
}

Solution

  • I got the solution, we have to do something like below:

    browser
      .chain
      .setSpeed(200)
      .session()
      .open('/')
      .click("id=save")
      .focus(editor)
      .and(function (browser) {
        for (var i = 0; i < 10; i++) {
          browser.keyDown(editor, '\\40')
        }
      })
      ...