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
- Basic idea of Selenium IDE
- Basic knowledge of Java.
- 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
- start()
- open()
- click()
- waitForPageToLoad()
- isTextPresent()
- assertTrue()
- type()
- isElementPresent()
- getEval()
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
Solution:
See the java code below after converting the recorded HTML script in IDE to Junit:
Helpful Video:
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:
this example does not use selenium 2
ReplyDeleteExample is solved without using Selenium 2 API
ReplyDeleteI'm getting an error on lines 51 and 52.
ReplyDeleteThe error I'm getting is: "Date cannot be resolved to a type".
I also get that 'SeleneseTestCase' is deprecated(line 9).
Its done...
ReplyDeleteYes, this one is working fine now.
ReplyDeleteThis example uses Selenium 1 api. The aim of this exercise is to convert from using Selenium IDE to Selenium Api. It does not use the latest Selenium 2 api
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete