如何在 Jenkins 上运行 Selenium/WebDriver 测试? [英] How to get Selenium/WebDriver tests running on Jenkins?

查看:40
本文介绍了如何在 Jenkins 上运行 Selenium/WebDriver 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Selenium 和自动化非常陌生.使用 Selenium IDE 和我对 Java 的一般知识,我能够在 Eclipse 中制作一系列在 JUnit 上运行的测试用例.现在我的测试当前在 eclipse 中运行并按 [run].我想将这些测试用例导入 Jenkins/Hudson.有两种方式我更喜欢做 CI.

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.

  1. 安排时间(每周一次)进行测试并发送结果电子邮件.

  1. Schedule a time (once per week) to run through the tests and send email of result.

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

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).

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

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.

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

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.

我的示例代码如下:

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;
    }
      }
}

推荐答案

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

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 :

  1. 确保您已经安装了 maven 并在您的系统中设置了 maven home.
  2. 使用构建自由风格的软件项目创建新项目.
  3. 转到系统中的 .jenkins 文件夹.如果您使用的是 mac,它将被放置在家里.将您的 selenium 项目复制并粘贴到自动化作业文件夹 (.jenkins/jobs/Automation/workspace) 中.应手动创建工作区文件夹.
  4. 创建作业/项目后,转到源代码管理并将其设置为无(我假设您目前正在使用本地系统中的 selenium 代码).如果你想从git仓库中抓取代码,你需要先添加git插件:Jenkins -> Manage Jenkins -> Manage Plugins-> 点击可用标签 -> 搜索 Github 插件.
  5. 检查 Build Triggers 部分下的 Build Periodically 并将其设置为 (0 7 * * *).这意味着您的自动化将在每天 7 点自动运行.
  6. 添加构建 以通过 maven 命令运行自动化 mvn清洁测试.如果您使用ma​​c/linux,请选择Execute Shell,如果您使用Windows 系统,请选择Execute Windows Batch Command.
  7. 如果您想发送自动化结果的电子邮件,您需要在 Jenkins-Manage Plugin 中安装 Email Ext Plugin 并设置可编辑电子邮件通知.如果有附件,maven工程结果一般放在target文件夹下.下面我为您提供了目标文件夹中的 zip 文件示例.
  8. 保存您的 jenkins/项目.
  9. 设置电子邮件发件人:转到 Jenkins -> 管理 Jenkins -> 配置系统并按照下面附加的步骤进行操作.
  10. 最后,构建您的 jenkins 项目/作业:
  11. 如果您还有任何问题,请告诉我;)
  1. Make sure you have already installed maven and set up the maven home in your system.
  2. Create new item with Build a free-style software project.
  3. Go to the .jenkins folder in your system. If you are using mac, it'll be placed inside home. Copy and paste your selenium project inside the Automation job folder (.jenkins/jobs/Automation/workspace). The workspace folder should be created manually.
  4. After creating the job/project, go to the Source Code Management and set it as none (I assume currently you are using your selenium code from your local system). If you want to grab the code from git repository, you need to add the git plugin first : Jenkins -> Manage Jenkins -> Manage Plugins -> click on Available tab -> search for Github Plugin.
  5. Check the Build Periodically under Build Triggers part and set it to (0 7 * * *). It means your automation will automatically be run at 7 everyday.
  6. Add build to run your automation through maven command mvn clean test. Please choose Execute Shell if you use mac/linux, or choose Execute Windows Batch Command if you use Windows system.
  7. If you want to send the email of the automation result, you need to install the Email Ext Plugin in Jenkins-Manage Plugin and set up the Editable Email Notification. If you have the attachment, the maven project result usually placed under the target folder. Below I give you example of zip file that I have in target folder.
  8. Save your jenkins/project.
  9. Set up the email sender : Go to Jenkins -> Manage Jenkins -> Configure System and follow the steps attached below.
  10. Finally, build your jenkins project/job :
  11. Let me know if you still have any issue ;)

这篇关于如何在 Jenkins 上运行 Selenium/WebDriver 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆