Thursday, 21 July 2011

Example - Cucumber with Ruby

Aim:
Running scripts using the integration of Cucumber with Ruby.


Difficulty Level: High

Pre-requisites Steps involved:
  • Download Ruby from link http://www.ruby-lang.org/en/downloads/ and install it and set the path into environment variable.
  • Assume that all the code files save in hard disk.


Steps to Implementation:

  1. Create a directory c:\Demo.
  2. Create a subdirectory c:\Demo\features
  3. Create a 3 subdirectory into “regression”,” step_definitions” and “support” into “features” directory.
  4. Create 2 subdirectory “pages” and “patches” into “support” directory.
  5. You can aloso download the project from here


Codes:
  • Create “cucumber.yml” file under “Demo” folder and put following code into “cucumber.yml” file:

default: -r features/support/patches/ -r features/step_definitions

  • Create “Gemfile” file under “Demo” folder and put following code into “Gemfile” file and change the file extension is .FILE type.

source "http://www.rubygems.org"

gem 'activesupport'
gem 'builder'
gem 'capybara'
gem 'childprocess'
gem 'cucumber'
gem 'diff-lcs'
gem 'ffi'
gem 'gherkin'
gem 'gizmo'
gem 'json'
gem 'mime-types'
gem 'multi_json'
gem 'nokogiri'
gem 'rack'
gem 'rack-test'
gem 'rake'
gem 'rspec'
gem 'rspec-core'
gem 'rspec-expectations'
gem 'rspec-mocks'
gem 'rubyzip'
gem 'selenium-webdriver'
gem 'term-ansicolor'
gem 'tilt'
gem 'xpath'


  • Create “search.feature” file under “regression” folder and put following code into “search.feature” file:

Feature: A simple Time2test

Scenario: A simple Time2test scenario
Given I am on the main Time2test
And I using Username "admin"
And I using Password "time4test"
And I click on the Log In button


  • Create “search_steps.rb” file under “step_definitions” folder and put following code into “search_steps.rb” file:

require "selenium-webdriver"
require 'Cucumber'
Given /^I am on the main Time2test$/ do
visit "http://foundation.time2test.co.uk/wp-login.php"

end

When /^I using Username "([^"]*)"$/ do |query|
on_page_with :Time2test do |page|
page.fill_in_user_login query

end
end
Then /^I using Password "([^"]*)"$/ do |query1|
on_page_with :Time2test do |page|
page.fill_in_user_pass query1

end
end


Then /^I click on the Log In button$/ do
on_page_with :Time2test do |page|
page.click_submit_button
end
end

  • Create “page_with_qastuffs.rb” file under “pages” folder and put following code into “page_with_qastuffs.rb” file:

module PageWithQastuffs
include Gizmo::PageMixin

def valid?
title.should =~ /.*time2test.co.uk*/
end

def title
@document.xpath("//title").inner_text
end

end

  • Create “page_with_time2test.rb” file under “pages” folder and put following code into “page_with_time2test.rb” file:

module PageWithTime2test
include Gizmo::PageMixin

def valid?
if title =~ /.*time2test.co.uk*/
return true
else
return false
end
end

def title
@document.xpath("//title").inner_text
end

def fill_in_user_login query
fill_in 'user_login', :with=> query
end

def fill_in_user_pass query1
fill_in 'user_pass', :with=> query1
end

def click_submit_button
click_on 'wp-submit'
end

def qastuffs_main_css
".description"
end



end

  • Create “env.rb” file under “support” folder and put following code into “env.rb” file:

require 'rspec/expectations'
require 'uri'
require 'net/http'
require "selenium-webdriver"
require 'capybara'
require 'capybara/dsl'
require File.dirname(__FILE__) + '/patches/capybara.rb';

#CAPYBARA
require 'capybara/cucumber'
require 'capybara/session'

#require 'features/support/patches/capybara'
Capybara.default_driver = :selenium
Capybara.run_server = false
Capybara.default_selector = :css
Capybara.default_wait_time = 10
#driver = Selenium::WebDriver.for :firefox
#gizmo!!
require 'gizmo'
World(Gizmo::Helpers)

Gizmo.configure do |config|
config.mixin_dir = File.dirname(__FILE__) + '/../pages'
end

  • Create “capybara.rb” file under “patches” folder and put following code into “capybara.rb” file:


include Capybara
include Capybara::DSL
if Object.const_defined? :Capybara
module Capybara
    alias :response :page
end
end





Run the command:
  1. Open the command prompt.
  2. Select the Project directory Demo.
  3. Run the command below commands.
  • gem install bundler
  • bundle install
  • bundle exec cucumber -r features/support/ -r features/step_definitions

Helpful Video File:

Tuesday, 19 July 2011

Example - Selenium 2 with Maven Tool

Aim:
How to work on Selenium 2 with Maven tool.

Difficulty Level: High

Questions?

  1. What is Maven tool?

Exercise Execution Time: more details coming soon

Steps

  1. First download Maven from following URL: http://maven.apache.org/download.html.
  2. Unzip the downloaded folder into ‘Program Files’ directory (not mandatory).
  3. Set M2 is “C:\Program Files\apache-maven-3.0\bin” and M2_HOME is “C:\Program Files\apache-maven-3.0” as environmental variable into your system.
  4. Set the path “C:\Program Files\apache-maven-3.0\bin” into System variables.
  5. Now Create a Selenium Project directory.
  6. Put the “pom.xml” file into Project Directory.
  7. Create some Scripts and put into Project Directory.
  8. Again create a directory into Project directory and put into “testng.xml” file.
  9. Now run the ‘mvn install’ command into command prompt and it gets executed successfully.


Example - Selenium 2.0 using WebDriver API

Aim:
Example to demonstrate Selenium 2.0 using Webdriver API, here are the aims of examples:

  1.  to setup the development environment - Eclipse , jUnit and Selenium 2
  2. to run and execute your first selenium 2 test script
  3. get a feel for the commands and execution on Eclipse



Difficulty Level: High

Exercise Execution Time: more details coming soon

Prerequisites:
  1. None - this example will demonstrate installation of all the executables
Commands used in example:
  • Driver.manage()
  • Driver.get(baseUrl)
  • Driver.findElement by id, name, linkText and xpath
  • clear()
  • sendkeys()
  • click()
  • random string
Steps in implementation:
  1. Download and Install Eclipse IDE using link http://www.eclipse.org/downloads/
  2. Tip: download the version suitable for your PC ( 32 bit/64bit))
  3. Download the Selenium2 API 'selenium-server-standalone-*.jar' library using link http://seleniumhq.org/download
  4. No need to get JUnit jar file in separate as it is inbuilt in  'selenium-server-standalone-*.jar'.
  5. Launch the Eclipse IDE
  6. Create a new Java project and name it FirstExample. You will see a default folder 'src' inside it.
  7. Do right click on the created project and select Build Path >> Configuration Build Path
  8. Add 'selenium-server-standalone-*.jar' under Libraries tab by clicking 'Add External Jars'.
  9. Right click on 'src' and add a package 'com.example.tests'.
  10. Create two java classes  'addPost.java' and 'addUser.java' into the created package.
  11. Paste the below mentioned two script in corresponding java files.
  12. Right click 'addPost.java' and click 'Run as >> JUnit test'.
What does script simulate?
    1. login using username and password to foundation.time2test.co.uk wordpress portal
    2. click "posts"
    3. Click on "add new"
    4. enter details in form for new post like title,
    5. click on "publish"
    6. click on "users"
    7. enter details for new user in form
    8. click on add user
Java Files: Save as addPost.java
    package com.example.tests;
    
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class addPost {
    private WebDriver driver;
    // Initializing the parameters
    private String baseUrl="http://foundation.time2test.co.uk/";
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
    // Initializing the driver that script would run the Firefox
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    
    @Test
    public void testSelenium2Example() throws Exception {
    // opening the base URL using the driver's get command
    driver.get(baseUrl);
    // Login into the application using the valid credential
    driver.findElement(By.linkText("Log in")).click();
    driver.findElement(By.id("user_login")).clear();
    driver.findElement(By.id("user_login")).sendKeys("admin");
    driver.findElement(By.id("user_pass")).clear();
    driver.findElement(By.id("user_pass")).sendKeys("time4test");
    driver.findElement(By.id("wp-submit")).click();
    
    // Asserting if we have logged into the application or not
    for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.xpath("//div[@id='user_info']/p/a[2]"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
    }
    
    // Clicking the Post link for adding a new post
    driver.findElement(By.linkText("Posts")).click();
    for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (isElementPresent(By.xpath("//div[@id='wpbody-content']/div[3]/h2"))) break; } catch (Exception e) {}
    Thread.sleep(1000);
    }
    // Add a new post
    driver.findElement(By.linkText("Add New")).click();
    driver.findElement(By.id("title")).clear();
    driver.findElement(By.id("title")).sendKeys("MyPost New Post");
    driver.findElement(By.id("publish")).click();
    // calling a method for adding an user
    addUser se=new addUser();
    se.seleniumExample(driver);
    
    }
    
    @After
    public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
    fail(verificationErrorString);
    }
    }
    
    private boolean isElementPresent(By by) {
    try {
    driver.findElement(by);
    return true;
    } catch (NoSuchElementException e) {
    return false;
    }
    }
    }
    
    
Save as addUser.java
    package com.example.tests;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.*;
    import java.util.*;
    
    public class addUser {
    
    public void seleniumExample(WebDriver driver)
    {
    driver.findElement(By.linkText("Users")).click();
    // generating a random value
    driver.findElement(By.xpath("//li[@id='menu-users']/div[3]/ul/li[2]/a")).click();
    Random rand = new Random();
    int numNoRange = rand.nextInt();
    String userstr="TestNewUser"+numNoRange;
    String email="Testemail"+numNoRange+"@yahoo.com";
    driver.findElement(By.id("user_login")).clear();
    // Using the random values in adding a new user
    driver.findElement(By.id("user_login")).sendKeys(userstr);
    driver.findElement(By.id("email")).clear();
    driver.findElement(By.id("email")).sendKeys(email);
    // Adding an user
    driver.findElement(By.id("first_name")).clear();
    driver.findElement(By.id("first_name")).sendKeys("TestFirst");
    driver.findElement(By.id("last_name")).clear();
    driver.findElement(By.id("last_name")).sendKeys("TestLast");
    driver.findElement(By.id("url")).clear();
    driver.findElement(By.id("url")).sendKeys("http://www.google.com/");
    driver.findElement(By.id("pass1")).clear();
    driver.findElement(By.id("pass1")).sendKeys("time2test");
    driver.findElement(By.id("pass2")).clear();
    driver.findElement(By.id("pass2")).sendKeys("time2test");
    driver.findElement(By.id("addusersub")).click();
    }
    }
Helpful Video: