如何在Jenkins上运行Selenium/WebDriver测试?

use*_*183 1 ant junit selenium jenkins

我是Selenium和Automation的新手.使用Selenium IDE和我对Java的一般知识,我能够在Eclipse中生成一系列在JUnit上运行的测试用例.现在我的测试目前在我日食时运行并按下[run].我想将这些测试用例导入Jenkins/Hudson.有两种方法我更喜欢做CI.

  1. 安排一个时间(每周一次)来完成测试并发送结果的电子邮件.

  2. 将我的测试用例上传到GitHub上的存储库,当存储库发生更改时,运行测试和/或按计划(每周一次).

老实说,我试图查找教程(视频/文件),但他们似乎都很不清楚.举个例子,我不知道build.xml或POM是什么.

使用Jenkins插件或使用ANT或Maven更好吗?如果是这样,我需要在代码中添加/更改以允许这种情况发生的事情,并在Jenkins中进行配置.

我的示例代码如下:

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;
    }
      }
}
Run Code Online (Sandbox Code Playgroud)

Rat*_*ari 5

根据您的信息,您可以使用maven项目库创建selenium自动化.在这种情况下,如果您想使用Jenkins作为CI,请执行以下步骤:

  1. 确保您已经安装了maven并在系统中设置了maven home.
  2. 使用构建自由式软件项目创建新项目.在此输入图像描述
  3. 转到系统中的.jenkins文件夹.如果您使用的是Mac,它将被放置在家中.将您的selenium项目复制并粘贴到自动化作业文件夹(.jenkins/jobs/Automation/workspace)中.应手动创建工作区文件夹.
  4. 创建作业/项目后,转到源代码管理并将其设置为none(我假设您当前正在使用本地系统的selenium代码).如果你想从git仓库获取代码,你需要先添加git插件:Jenkins - > Manage Jenkins - > Manage Plugins - >点击Available选项卡 - >搜索Github插件.
  5. Build Triggers部分下定期检查Build并将其设置为(0 7***).这意味着您的自动化将每天自动运行7次.
  6. 添加构建 步通过maven命令运行自动化mvn clean test.如果使用mac/linux,请选择Execute Shell;如果使用Windows系统,请选择Execute Windows Batch Command.
  7. 如果要发送自动化结果的电子邮件,则需要在Jenkins-Manage插件中安装Email Ext插件并设置可编辑的电子邮件通知.如果您有附件,maven项目结果通常放在目标文件夹下.下面我给你一个我在目标文件夹中的zip文件的例子.
  8. 保存你的jenkins /项目.在此输入图像描述
  9. 设置电子邮件发件人:转到Jenkins - >管理Jenkins - >配置系统,然后按照下面的步骤操作.在此输入图像描述
  10. 最后,构建你的jenkins项目/工作: 在此输入图像描述
  11. 如果您还有任何问题,请告诉我;)