Automation Testing
Selenium webdriver
Muhammad Bilal
Agenda
❏What is selenium and its architecture?
❏What is Mocha Framework?
❏Selenium webdriver API commands and operations.
❏Setting Up a selenium webdriver project.
❏Creating tests using selenium.
Selenium
It is for automated testing of web applications.
Browsers Support:
IE, FireFox, Opera and Chrome.
Language Support:
C#, Java, Perl, PHP, Python, Ruby, Javascript.
Components:
Selenium IDE, Selenium RC, Selenium Grid, Selenium Webdriver
Architecture
Advantages Of Automation Testing
➢Automation saves time and effort to repeating same tests over and over
again.
➢Execute test script at any time often with different data.
➢Automation is also essential for regression testing. (ensure already
implemented features are not broken)
Mocha
➢Mocha is a JavaScript test framework running on node.js.
➢Browser support
➢Asynchronous testing
➢Test coverage reports
➢Use of any assertion library (assert, chai)
Why
To make sure our test is passed or failed based on some conditions. Each
test case must have some assertions.
Selenium APIs
➢Fetching a page
driver.get('http://192.168.2.101:9092');
➢Locating UI Elements By ID
<div id="coolestWidgetEvah">...</div>
driver.findElement(By.id('coolestWidgetEvah'));
➢By className
<div class="cheese">.......</div>
driver.findElements(By.className("cheese"));
➢By tagName
var frame = driver.findElement(By.tagName('iframe'));
➢By Name
driver.findElement(By.name('cheese'));
➢By linkTesxt
<a href="http://www.google.com/search?q=cheese">cheese</a>>
driver.findElement(By.linkText('cheese'));
➢By css
<div id="food"><span class="dairy">milk</span><p> …. </p></div>
driver.findElement(By.css('#food p'));
➢By xPath
driver.findElements(By.xpath("//input"));
➢Getting Text
driver.findElement(By.id('elementID')).getText().then(text => console.log(`Text is `));
➢Submit / click
driver.findElement(By.id('elementID')).click() / .submit();
➢sendKeys
driver.findElement(webdriver.By.name('username')).sendKeys('admin');
Setting Up Selenium using Javascript
Install javascript bindings with npm, nodejs version > 4.1
➢npm install selenium-webdriver
➢npm install mocha chai -g
Goto directory where selenium-webdriver is installed
➢Make a new javascript file with any name like (selenium.js)
➢Configure selenium-webdriver
var webdriver = require('selenium-webdriver'),
By = require('selenium-webdriver').By,
until = require('selenium-webdriver').until;
var driver = new webdriver.Builder().forBrowser('firefox') .build();
Test Case
➢Webdriver should open cbsgui and enter username admin and
password admin and press login button.
dashboard page should open and logout link should appear on
dashboard.
➢Steps:
Open cbsgui (192.168.2.101:9092)
Find element username,password and enter data.
Submit login request.
➢Expected Result:
Moved to dashboard page.
Script Using Mocha
var webdriver = require('selenium-webdriver'),
By = require('selenium-webdriver').By,
until = require('selenium-webdriver').until;
var test = require('./node_modules/selenium-webdriver/testing');
var expect = require('chai').expect;
test.describe('se-Demo', function() { // define test suite
this.timeout(1000 * 100);
var driver;
test.before(function() {
driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
});
test.after(function() {
driver.quit();
});
test.it('Should Login Successfully', function(done) {
driver.get('http://192.168.2.101:9092').then(function () {
driver.findElement(webdriver.By.name('username')).sendKeys('admin');
element = driver.findElement({name: 'password'});
element.sendKeys('admin'); element.submit();
}) .then(function () {
driver.wait(until.elementLocated(By.className('fa-sign-out')), 1000 * 60);
expect(driver.findElement(By.className('fa-sign-out'))).not.to.be.null;
done();
});
});
});
References
1. http://www.seleniumhq.org/docs/03_webdriver.jsp
2. http://seleniumhq.github.io/selenium/docs/api/javascript/module/seleniu
m-webdriver/lib/promise.html
3. https://gist.github.com/huangzhichong/3284966
4. https://mochajs.org/
5. http://chaijs.com/api/bdd/
Thank You
Any Question ?

Selenium Webdriver

  • 1.
  • 2.
    Agenda ❏What is seleniumand its architecture? ❏What is Mocha Framework? ❏Selenium webdriver API commands and operations. ❏Setting Up a selenium webdriver project. ❏Creating tests using selenium.
  • 3.
    Selenium It is forautomated testing of web applications. Browsers Support: IE, FireFox, Opera and Chrome. Language Support: C#, Java, Perl, PHP, Python, Ruby, Javascript. Components: Selenium IDE, Selenium RC, Selenium Grid, Selenium Webdriver
  • 4.
  • 5.
    Advantages Of AutomationTesting ➢Automation saves time and effort to repeating same tests over and over again. ➢Execute test script at any time often with different data. ➢Automation is also essential for regression testing. (ensure already implemented features are not broken)
  • 6.
    Mocha ➢Mocha is aJavaScript test framework running on node.js. ➢Browser support ➢Asynchronous testing ➢Test coverage reports ➢Use of any assertion library (assert, chai) Why To make sure our test is passed or failed based on some conditions. Each test case must have some assertions.
  • 7.
    Selenium APIs ➢Fetching apage driver.get('http://192.168.2.101:9092'); ➢Locating UI Elements By ID <div id="coolestWidgetEvah">...</div> driver.findElement(By.id('coolestWidgetEvah')); ➢By className <div class="cheese">.......</div> driver.findElements(By.className("cheese")); ➢By tagName var frame = driver.findElement(By.tagName('iframe')); ➢By Name driver.findElement(By.name('cheese'));
  • 8.
    ➢By linkTesxt <a href="http://www.google.com/search?q=cheese">cheese</a>> driver.findElement(By.linkText('cheese')); ➢Bycss <div id="food"><span class="dairy">milk</span><p> …. </p></div> driver.findElement(By.css('#food p')); ➢By xPath driver.findElements(By.xpath("//input")); ➢Getting Text driver.findElement(By.id('elementID')).getText().then(text => console.log(`Text is `)); ➢Submit / click driver.findElement(By.id('elementID')).click() / .submit(); ➢sendKeys driver.findElement(webdriver.By.name('username')).sendKeys('admin');
  • 9.
    Setting Up Seleniumusing Javascript Install javascript bindings with npm, nodejs version > 4.1 ➢npm install selenium-webdriver ➢npm install mocha chai -g Goto directory where selenium-webdriver is installed ➢Make a new javascript file with any name like (selenium.js) ➢Configure selenium-webdriver var webdriver = require('selenium-webdriver'), By = require('selenium-webdriver').By, until = require('selenium-webdriver').until; var driver = new webdriver.Builder().forBrowser('firefox') .build();
  • 10.
    Test Case ➢Webdriver shouldopen cbsgui and enter username admin and password admin and press login button. dashboard page should open and logout link should appear on dashboard. ➢Steps: Open cbsgui (192.168.2.101:9092) Find element username,password and enter data. Submit login request. ➢Expected Result: Moved to dashboard page.
  • 11.
    Script Using Mocha varwebdriver = require('selenium-webdriver'), By = require('selenium-webdriver').By, until = require('selenium-webdriver').until; var test = require('./node_modules/selenium-webdriver/testing'); var expect = require('chai').expect; test.describe('se-Demo', function() { // define test suite this.timeout(1000 * 100); var driver;
  • 12.
    test.before(function() { driver =new webdriver.Builder() .forBrowser('firefox') .build(); }); test.after(function() { driver.quit(); });
  • 13.
    test.it('Should Login Successfully',function(done) { driver.get('http://192.168.2.101:9092').then(function () { driver.findElement(webdriver.By.name('username')).sendKeys('admin'); element = driver.findElement({name: 'password'}); element.sendKeys('admin'); element.submit(); }) .then(function () { driver.wait(until.elementLocated(By.className('fa-sign-out')), 1000 * 60); expect(driver.findElement(By.className('fa-sign-out'))).not.to.be.null; done(); }); }); });
  • 14.
    References 1. http://www.seleniumhq.org/docs/03_webdriver.jsp 2. http://seleniumhq.github.io/selenium/docs/api/javascript/module/seleniu m-webdriver/lib/promise.html 3.https://gist.github.com/huangzhichong/3284966 4. https://mochajs.org/ 5. http://chaijs.com/api/bdd/
  • 15.