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

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

问题描述

我对Selenium和Automation非常陌生.使用Selenium IDE和我对Java的一般知识,我能够在Eclipse上进行一系列在JUnit上运行的测试用例.现在,当我在月食中运行我的测试时,请按[运行].我想将这些测试用例导入Jenkins/Hudson.我希望通过两种方式进行配置项.

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项目库创建硒自动化.在这种情况下,如果要使用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,它将放在家里.将您的硒项目复制并粘贴到自动化作业文件夹(.jenkins/jobs/Automation/工作区)中.工作区文件夹应手动创建.
  4. 创建作业/项目后,转到源代码管理,并将其设置为none(我假设您当前正在使用本地系统中的硒代码).如果要从git存储库中获取代码,则需要先添加git插件: Jenkins -> Manage Jenkins -> Manage Plugins ->单击可用标签->搜索 Github插件.
  5. 检查构建触发器部分下的定期构建,并将其设置为(0 7 * * *).这意味着您的自动化将每天7点自动运行.
  6. 添加内部版本 ,以通过Maven命令 mvn运行自动化干净的测试.如果使用 mac/linux ,请选择 Execute Shell ;如果使用 Windows 系统,请选择 Execute Windows Batch Command .
  7. 如果要发送自动化结果的电子邮件,则需要在Jenkins-Manage插件中安装 Email Ext插件,并设置可编辑电子邮件通知.如果有附件,则maven项目结果通常放置在目标文件夹下.下面,我为您提供目标文件夹中具有zip文件的示例.
  8. 保存您的jenkins/项目.
  9. 设置电子邮件发件人:转到Jenkins-> Manage 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天全站免登陆