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?
Exercise Steps:
Zip file location: ( download from here)
C:\FoundationSelenium\JavaBasedScript\QuickTest.java.
Scripts:
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
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
- Basic idea of Selenium IDE
- Basic knowledge of Java.
- Can access Wordpress.com and is familiar with Familiarise yourself with Wordpress.com?
- Eclipse installed
- Selenium RC server
- Firefox and Selenium IDE installed
- Download Junit 4.X.jar from http://www.junit.org/
- selenium-server.jar, selenium java client driver. jar from http://seleniumhq.org/download/ ( current version is 2.6.0)
- add the jar files to eclipse
Exercise Steps:
- Use the IDE to record a simple script
- convert into Junit/Java Formatted Code using the IDE
- Save the code QuickTest.java on the harddrive
- Launch Eclipse
- Create a Java project directory, see the attached video file for how to create Java project in Eclipse
- Move QuickTest.java to the project directory
- Start Selenium RC Server via command line
- Execute QuickTest.java via JUnit
- See the Result
- 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
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
I have uploaded the helpful video file but still I need to put some more details in terms of Script, Tips, zip etc.
ReplyDeleteIt's done, few things need to be done on VM:
ReplyDeleteI 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.
Is this script still working? I have tried on my machine but I get some errors.
ReplyDeleteI have watched thwe video and followed all the instructions, but is there something missing ?
Actually it seems to work if I remove the lest Selenium test about creating a new user. Any ideas why this is ?
ReplyDeleteI have rectified the problem,can you try again? Let me know if you face any issue again?
ReplyDeleteNote. This example uses Selenium 1 and not selenium2/webdriver.
ReplyDeleteselenium 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.
ReplyDeleteHi Viresh,
ReplyDeleteWhat 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”
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.
DeleteHi
Deleteyou 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!)