Showing posts with label Additional. Show all posts
Showing posts with label Additional. Show all posts

Thursday, 24 November 2011

Example - Selenium Grid2

Aim - To demonstrate how to run multiple tests on multiple browsers (IE and FF) in parallel using Selenium Grid2. The Selenium scripts which are written in java will open up Google.com and Search for the keywords.

Difficulty Level: HIGH

Pre-requisites:

  • JDK (JAVA version "1.6.021") should be installed and working
  • Ant (Apache Ant version 1.7.1) should be working
  • selenium-server-standalone-2.12.0.jar
  • testng-5.14.1.jar

Wednesday, 9 November 2011

Example : Working with dbunit using junit

Aim:
To demonstrate DBunit with Junit by inserting a record in the database then using dbunit to assert for the uploaded records in a database/ table.

Difficulty Level: High

Pre-Requisites:
  • Eclipse IDE installed
  • MY SQL Server should be installed and working
  • dbunit, junit, java mysql connector jar files available
Steps involved in implementation:
  • Create a database / table using My sql database server
  • Connect with created database's table using java My SQL connection code
  • In the connected table upload records through XML file using DBunit methods
  • The XML shows the number of records we have uploaded into the table
  • With the help of dbunit methods we assert for the uploaded records in table

Friday, 21 October 2011

Example-Cross Browser Testing

Aim:To run the Script on different browser.
Difficulty Level: High

Prerequisites:
  • Eclipse IDE installed
  • selenium-grid-tools, selenium java client driver. jar from http://seleniumhq.org/download/
  • Selenium RC server
  • Firefox,Chrome and IE installed
Commands used in example:
  • Driver.manage()
  • Driver.get(baseUrl)
  • Driver.findElement by id, name, linkText,cssSelector
  • clear()
  • sendkeys()
  • click()
  • random string
Steps in implementation:
  1. Create a Java Project in Eclipse IDE.
  2. Now copy the script and save as CrossTest.java
  3. Include all the jar files with the created project.
  4. Start Selenium RC Server via command line.
  5. For running the same test we need to just change the driver like below script is for the Firefox driver and will run on same, so just running the script on IE, we need to load the IE browsers driver
  6. For IE ->
    IWebDriver driver = new InternetExlorerDriver();
  7. For Google Chrome ->
    IWebDriver driver = new ChromeDriver();
  8. See the Result

Thursday, 20 October 2011

FirstExample - Selenium 2 using WebDriver API

Aim:Example to demonstrate Selenium 2 using Webdriver API.
Difficulty Level: High


Prerequisites:
  1. Eclipse IDE installed
Commands used in example:
  • Driver.manage()
  • Driver.get(baseUrl)
  • Driver.findElement by id, name, link text, css and xpath
  • IsElementPresent()
  • clear()
  • sendkeys()
  • click()
  • random string
  • assertTrue()
  • getEval()
Steps in implementation:
  1. In first step, created a Java Project directory On Eclipse IDE.
  2. In second step. copy all the scripts and save as .java formate.
  3. Downloaded Selenium 2 Library.
  4. Included all the downloaded libraries with the project’s directoryaccording to video.
  5. Run the scripts successfully.

Scripts:
Save as Suite.java
package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Suite {
private WebDriver driver;
private String baseUrl="http://foundation.time2test.co.uk/";
public StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSuite() throws Exception {
Login ln=new Login(driver);
ln.loginAs();
PostBlog post=new PostBlog(driver);
post.newPost();
AddUser user=new AddUser(driver);
user.newUser();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}

}


Save as Login.java
package com.example.tests;
import org.openqa.selenium.*;
import org.testng.Assert;
import com.thoughtworks.selenium.Selenium;

public class Login {
private WebDriver driver;
public Login(WebDriver driver) {
this.driver = driver;
}
public void loginAs() {
driver.get("http://foundation.time2test.co.uk/");
Selenium selenium = new WebDriverBackedSelenium(driver, "http://foundation.time2test.co.uk/");
driver.findElement(By.linkText("Log in")).click();
driver.findElement(By.id("user_login")).clear();
Assert.assertTrue(selenium.isTextPresent("Username"));
Assert.assertTrue(selenium.isTextPresent("Password"));
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();
try{
Thread.sleep(3000);
}catch(Exception e){}
}
}

Save as AddUser.java
package com.example.tests;
import org.openqa.selenium.*;
import com.thoughtworks.selenium.Selenium;
import java.util.*;


public class AddUser {
private WebDriver driver;
public AddUser(WebDriver driver) {
this.driver = driver;
}
public void newUser() throws Exception
{
driver.findElement(By.linkText("Users")).click();

Random rand = new Random();
int numNoRange = rand.nextInt();
String userstr="TestNewUser"+numNoRange;
String email="Testemail"+numNoRange+"@yahoo.com";
driver.findElement(By.xpath("//li[@id='menu-users']/div[3]/ul/li[2]/a")).click();

driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys(userstr);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(email);

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();
DeleteUser del=new DeleteUser(driver);
del.delUser(userstr);
}

}

Save as DeleteUser.java
package com.example.tests;

import org.openqa.selenium.*;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.WebElement;

public class DeleteUser {
private WebDriver driver;
public DeleteUser(WebDriver driver) {
this.driver = driver;
}
public void delUser(String user) throws Exception
{
Selenium seleniumDriver = new WebDriverBackedSelenium(driver, "http://foundation.time2test.co.uk/");
driver.findElement(By.cssSelector("a.wp-first-item.current")).click();
driver.findElement(By.id("user-search-input")).clear();
driver.findElement(By.id("user-search-input")).sendKeys(user);
driver.findElement(By.cssSelector("input.button")).click();
driver.findElement(By.xpath("//div[@id='wpwrap']/div[@id='wpcontent']/div[@id='wpbody']/div[@id='wpbody-content']/div[3]/form[@id='posts-filter']/table/thead/tr/th[@id='cb']/input")).click();
WebElement element = driver.findElement(By.name("action"));
/*seleniumDriver.select("name=action", "label=Delete");
seleniumDriver.click("id=doaction");
seleniumDriver.waitForPageToLoad("30000");
*/

element.sendKeys("Delete");
driver.findElement(By.id("doaction")).click();
driver.findElement(By.name("submit")).click();
}

}



Save as PostBlog.java
package com.example.tests;
import org.openqa.selenium.*;
import com.thoughtworks.selenium.Selenium;


public class PostBlog {
private WebDriver driver;
public PostBlog(WebDriver driver) {
this.driver = driver;
}
public void newPost()throws Exception
{
Selenium selenium = new WebDriverBackedSelenium(driver, "http://foundation.time2test.co.uk/");
driver.findElement(By.linkText("Posts")).click();
try{
Thread.sleep(3000);
}catch(Exception e){}

driver.findElement(By.linkText("Add New")).click();
driver.findElement(By.id("title")).clear();
selenium.type("id=title", selenium.getEval(""Post" + new Date().getMilliseconds();"));
driver.findElement(By.id("publish")).click();
driver.findElement(By.cssSelector("a.wp-first-item.current")).click();
}

}




Helpful Video:

Monday, 17 October 2011

Example: First selenium script from End to End

Aim:
To create java selenium Scripts using a IDE and execute them in Eclipse in less than 10 steps. Here we are using the IDE to create java code which is different from using webdriver which a richer and more flexible api.

Difficulty level: Medium

What you will learn?
  • How to switch from IDE to Selenuium RC using Java
  • Basics of the advanced IDE Eclipse Setup
  • Introduction to the Selenium Java Client Driver
  • Introduction to JUnit Api
Tips:
  • Basic idea of Selenium IDE
  • Basic knowledge of Java.
Pre-requisites:
  • Eclipse installed
  • Selenium RC server
  • Firefox and Selenium IDE installed
  • add the jar files to eclipse
Commands used in example:
  • start()
  • open()
  • click()
  • waitForPageToLoad()
  • isTextPresent()
  • assertTrue()
  • type()
  • isElementPresent()
  • getEval()

Exercise Steps:
1. Use the IDE to record a simple script
2. convert into Junit/Java Formatted Code using the IDE
3. Save the code QuickTest.java on the harddrive
4. Launch Eclipse
5. Create a Java project directory, see the attached video file for how to create Java project in Eclipse
6. Move QuickTest.java to the project directory
7. Start Selenium RC Server via command line
8. Execute QuickTest.java via JUnit
9. See the Result


Conclusions
  • Installed Ecplise IDE and seen it working with a java selenium Example
  • Familiar with end to end advanced Selenium Script Design
  • Familiar with how to record a script using Selenium IDE and convert it to Java

Solution:
See the java code below after converting the recorded HTML script in IDE to Junit:
package com.example.tests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class QuickTest extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://foundation.time2test.co.uk/");
selenium.start();
}

@Test
public void testQuick() throws Exception {
selenium.open("/");
selenium.click("link=Log in");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Username"));
assertTrue(selenium.isTextPresent("Password"));
selenium.type("id=user_login", "admin");
selenium.type("id=user_pass", "time4test");
selenium.click("id=wp-submit");
selenium.waitForPageToLoad("30000");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("//div[@id='user_info']/p/a[2]")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

selenium.click("link=Posts");
selenium.waitForPageToLoad("30000");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("//div[@id='wpbody-content']/div[3]/h2")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

selenium.click("link=Add New");
selenium.waitForPageToLoad("30000");
selenium.type("id=title", "MyPost");
selenium.click("id=publish");
selenium.waitForPageToLoad("30000");
selenium.click("link=Users");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("Users"));
selenium.click("//li[@id='menu-users']/div[3]/ul/li[2]/a");
selenium.waitForPageToLoad("30000");
selenium.type("id=user_login", selenium.getEval(""Testuser" + new Date().getMilliseconds();"));
selenium.type("id=email", selenium.getEval(""Testemail" + new Date().getMilliseconds()+"@gmail.com";"));
selenium.type("id=first_name", "TestFirst");
selenium.type("id=last_name", "TestLast");
selenium.type("id=url", "http://www.google.com/");
selenium.type("id=pass1", "time2test");
selenium.type("id=pass2", "time2test");
selenium.click("id=addusersub");
selenium.waitForPageToLoad("30000");
selenium.click("link=Log Out");
selenium.waitForPageToLoad("30000");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

Helpful Video:


Wednesday, 21 September 2011

Robotium tool integration with TestDroid

Aim:
To create a script via TestDroid tool and run the created script using Robotium.

Difficulty Level: Hard

What you will learn?
How to run script into the Android emulator using integration of EclipseIDE, TestDroid and Robotium.


Questions?

  1. What is TestDroid?
  2. What is Robotium?

Tips:

  • Good knowledge of EcpliseIDE