I am very new to Selenium and Automation. Using Selenium IDE and my general knowledge of Java I was able to make a series of test cases in Eclipse that run on JUnit. Now my test currently run when I am in eclipse and press [run]. I would like to import these test cases to Jenkins/Hudson. There are two ways I would prefer doing the CI.
Schedule a time (once per week) to run through the tests and send email of result.
Upload my test cases to a repository on GitHub and when there is a change done to the repository, run the tests and/or on a schedule (once per week).
Ive honestly tried to look up tutorials (videos/documents) but they all seem very unclear. Just to give an example, I do not know what a build.xml or POM is.
Is it better to do this with a Jenkins Plugin or using ANT or Maven? If so what are the things I need to add/change in my code to allow this to happen, and configure in Jenkins.
My Example Code is Below:
package Profile;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import com.opera.core.systems.scope.protos.ExecProtos.ActionList.Action;
public class P_ProfileChangeTestCase {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
//Before the test begins, creates a new webdriver and sets the base url
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.test.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testOpen() throws Exception {
System.out.println("**Starting Profile**");
driver.get(baseUrl);
//Click LogIn
System.out.println("Clicking Log In");
driver.findElement(By.cssSelector("div.button.login > a.link")).click();
//Enter User name
System.out.println("Entering Username");
driver.findElement(By.xpath("//input[@id='login']")).sendKeys("TEST");
//Enter Password
System.out.println("Entering Password");
driver.findElement(By.xpath("//input[@id='login_password']")).sendKeys("PW");
//Click LogIn Button
System.out.println("Submit Log In");
driver.findElement(By.className("login-button")).click();
//Verify user name login by echo name to console
System.out.println("Verify User Log In");
String text = driver.findElement(By.cssSelector("span.username")).getText();
System.out.println("Username is :" + text);
////////////////////////
//Click on Edit Profile
System.out.println("Clicking on Edit Profile Button");
driver.findElement(By.cssSelector("div.button.login")).click();
driver.findElement(By.xpath("//div[@id='mlg-header']/div/div[3]/div/div[7]/div/div[2]/a")).click();
//Change description in profile
System.out.println("Editing the Interests section of profile");
driver.findElement(By.name("interests")).clear();
driver.findElement(By.name("interests")).sendKeys("Edit Profile in Selenium Eclipse");
//Update Profile
System.out.println("Click on submit to change profile");
driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
//Verify that update has been applied to profile
System.out.println("Verifing that change has been made");
assertEquals("Profile has been updated.", driver.findElement(By.cssSelector("b > b")).getText());
//Console Output of Assert Statement Above
System.out.println("Profile has been updated!");
System.out.println("**Profile Complete!**");
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
}
Based on your information, you create your selenium automation using maven project base. In this case, if you want to use Jenkins as your CI, here is the steps :
Based on the posted code and the fact that you are using Maven, I'd say "mvn clean test" will run all the unit tests. So create a meven project in Jenkins (for that you'll need maven plugin in Jenkins) and in the configuration provide "clean test" as your maven goals to be executed by the jenkins' project.
Looking at the bigger picture here, although your tests are annotated as unit tests, but they don't possess the characteristics of unit tests. For one, unit tests should Mock external dependencies.