This document provides steps to run a Selenium test script using TestNG in Eclipse:
1. Record a test script in Selenium IDE and export it as a Java file.
2. Create a Java project in Eclipse, import the exported file and add the Selenium WebDriver JAR files.
3. Install the TestNG plugin in Eclipse.
4. Modify the exported Java file to replace JUnit annotations and code with TestNG equivalents to run the script with TestNG.
5. Run the test by right clicking the Java file and selecting "Run as TestNG Test" and verify the results.
1 of 21
Download to read offline
More Related Content
About Selenium Webdriver
1. Run a simple test script in Selenium Webdriver with TestNG
Step 1 : Record selenium automation script using Selenium IDE.
2. Step 2: Export the recorded selenium automation script as a Java / JUnit 4 / Webdriver. Eg. Here we
export a java as ^Login.java ̄
a)Export as java file
12. j) After import the java file will been seen with error since the classpath not set for selenium webdriver jars.
13. Step 4 : Download Selenium 2 Web Driver from http://seleniumhq.org/download/
14. You will get the folder in your download path and keep it there and unzip it so that you can use it in later
steps. The files in the folder should look something like this :
17. Step 5: Add External Files (Referenced Libraries ) in to the Eclipse.
a. Right Click the project and go to Properties.
b. Go to Java Build Path -> Library tab -> Add External JARs´
c. Now add all the jar files
d. Check if they are appearing in the Eclipse as below.
18. e. After configure the classpath the error will be cleared automatically.
19. Step 6: Install TestNG Plug-in in Eclipse.
In Eclipse, go to Help -> Install New Software
Enter http://beust.com/eclipse in the Work with: field
Wait and TestNG will be appeared in the window. Check the box next to TestNG and Click on Next button. Click on
Finish and the Restart the Eclipse.
Step 7: Change the Junit code to TestNG code.(Exported Login.java)
? Change the annotation from Junit code to TestNG.
Eg. @Before replace with @BeforeTest
@After replace with @AfterTest
? Remove the Junit import header and replace with appropriate TestNG import header .
20. Eg., import org.junit.*; replace with import org.testng.*; and
import org.testng.annotations.*;
? If any junit code is used in then replace with appropriate TestNG code.
? verifyTextPresent in a field may require manual changes
Eg.
try {
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[sS]*Homern
rn[sS]*$"));
} catch (Error e) {
verificationErrors.append(e.toString());
}
replace with
try {
assertEquals(^Home ̄,driver.findElement(By.id("WindowName")).getText());
} catch (Error e) {
verificationErrors.append(e.toString());
}
Step 7 : Run the selenium automation script by right the java file and Run as-> TestNG Test.
Step 8 : Verify the test results in the console.
21. Note: In the exported Java from selenium IDE require manual changes to run the script in
eclipse.