Monday, 30 May 2011

Create java selenium in 10 steps and execute

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:
Advanced

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:


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:
Zip file location: ( download from here)
C:\FoundationSelenium\JavaBasedScript\QuickTest.java.

Scripts:
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:




Video File Steps

Record a script by Selenium IDE and change into Java Format and run on Selenium RC
1. start Selenium IDE
2. record a script on Selenium IDE against wordpress.com
3. click on Options->Format->jUnit 4 to covert the selenium script to Java format
4. Save test case as QuickTest.java
5. Now open Ecilpse IDE
6. create a java project
7. Create a package in that java project ( com.example.tests)
8. Now copy the previously aved test case into the package just created
9. Now include all the jar files
create a lib directory and add the jar file
10. Now lauch the selenium Server
java -jar selenium-server.jar
11. Now run the java project
12. Click on Run As - > jUnit Test



Related Entries:

http://time2testcourse.blogspot.com/2011/04/example-running-selenium-using.html

    10 comments:

    1. I have uploaded the helpful video file but still I need to put some more details in terms of Script, Tips, zip etc.

      ReplyDelete
    2. It's done, few things need to be done on VM:
      I have sent you a created project's zip on mail, please put this in "C:\WorkshopFiles\" path on VM.
      Also if WorkshopFiles folder is not there then please create it first so that in future only we will keep the referenced zips in solutions into the same folder.

      ReplyDelete
    3. Is this script still working? I have tried on my machine but I get some errors.

      I have watched thwe video and followed all the instructions, but is there something missing ?

      ReplyDelete
    4. Actually it seems to work if I remove the lest Selenium test about creating a new user. Any ideas why this is ?

      ReplyDelete
    5. I have rectified the problem,can you try again? Let me know if you face any issue again?

      ReplyDelete
    6. Note. This example uses Selenium 1 and not selenium2/webdriver.

      ReplyDelete
    7. selenium 1 requires you to start the selenium standalone server before executing the selenium java based scripts. Webdriver 2/ selenium 2 will start and manage the selenium server automatically which makes selenium 2 a better option going forward.

      ReplyDelete
    8. Hi Viresh,

      What I'm trying to do is to run a very simple 'record' in the Selenium IDE using IE. The reason that I want to run a simple record in IE is that I would like Selenium to generate the html code for the Test Suite File and Test Case file. In this way, I can manipulate these in Notepad++ and take my testing further from there. Is there a way of performing a recording using Selenium with IE or do I have to look at things from another angle? Thanks, Eoin.

      I have tried running the command below (in the directory I'm working in, having copied the the selenium server... .jar file into that directory), but no luck as there were 'access denied' errors but that looks like a problem on my side:

      java –jar selenium-server-standalone-2.25.jar –htmlSuite “*iehta” “http://” “C:\ .html” “C:\ .txt”

      ReplyDelete
      Replies
      1. I see that text that is entered between 'less than' and 'greater than' symbols is not published and hence my command line syntax looks funny. However, I'm not receiving Syntax errors.

        Delete
      2. Hi
        you can't use IE to record via Selenium IDE. The Selenium IDE is only available for recording on Firefox. THe access denied is usually is environment related, however feel free to send screenshots or messages and let's see what we can diagnose.
        (The comment box does not allow certain syntax!)

        Delete

    Thank you for your comment
    - Time2test Team