Date post: | 23-Jan-2017 |
Category: |
Documents |
Upload: | bilalmetla |
View: | 110 times |
Download: | 0 times |
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.
SeleniumIt 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 tagNamevar frame = driver.findElement(By.tagName('iframe'));
➢By Namedriver.findElement(By.name('cheese'));
➢switchTodriver.switchTo().window('windowName');
➢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 xPathdriver.findElements(By.xpath("//input"));
➢Getting Textdriver.findElement(By.id('elementID')).getText().then(text => console.log(`Text is `));
➢Submit / clickdriver.findElement(By.id('elementID')).click() / .submit();
➢sendKeysdriver.findElement(webdriver.By.name('username')).sendKeys('admin');
➢ navigateTo , alerts, sleep(), clear(), toggle()
Setting Up Selenium using JavascriptInstall 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-webdrivervar webdriver = require('selenium-webdriver'), By = require('selenium-webdriver').By, until = require('selenium-webdriver').until;var driver = new webdriver.Builder().forBrowser('firefox') .build();var test = require('./node_modules/selenium-webdriver/testing'); //(required only when writing tests with mocha)
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.Logout link should be appear.
Script Using Mochavar 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();
});
});
});
References1.http://www.seleniumhq.org/docs/03_webdriver.jsp
2.http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise.html
3.https://gist.github.com/huangzhichong/3284966
4.https://mochajs.org/
5.http://chaijs.com/api/bdd/
Thank YouAny Question ?