2014-01-30 112 views
1

我对Selenium和Automation很新。使用Selenium IDE和我对Java的一般知识,我能够在Eclipse中制作一系列在JUnit上运行的测试用例。现在我的测试在eclipse中运行,然后按[run]。我想将这些测试用例导入Jenkins/Hudson。有两种方法我更喜欢做CI。如何获得在Jenkins上运行的Selenium/WebDriver测试?

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

指正!我已经创建了一个maven项目,并且作为java文件,我有上面的以下代码。在我的POM XML我有硒依赖和junit依赖 – user2683183

回答

5

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

  1. 确保您已经安装了maven并在系统中设置了maven home。
  2. 创建新项目构建一个自由风格的软件项目enter image description here
  3. 转到您系统中的.jenkins文件夹。如果你正在使用mac,它将被放置在家中。复制并粘贴自动化作业文件夹内的硒项目(.jenkins/jobs/Automation/工作区)。工作区文件夹应该手动创建。
  4. 创建工作/项目后,转到源代码管理并将其设置为无(我假设您目前正在使用本地系统中的硒代码)。如果你想抓住从git仓库的代码,你需要首先添加的git插件:詹金斯 - >管理詹金斯 - >管理插件 - >点击可用选项卡 - >搜索Github的插件
  5. 检查定期建立根据构建触发器部分并将其设置为(0 7 * * *)。这意味着您的自动化将自动在每天7点运行。
  6. 添加版本step通过maven命令运行您的自动化mvn clean test。请选择执行shell如果使用MAC/Linux的,或选择执行的Windows批处理命令如果您使用的Windows 系统
  7. 如果你要发送的自动化结果的电子邮件,您需要安装电子邮件分机插件在詹金斯 - 管理插件,并成立了可编辑的电子邮件通知。如果你有附件,Maven项目结果通常放在目标文件夹下。下面我给你在目标文件夹中有的zip文件的例子。
  8. 保存你的jenkins /项目。 enter image description here
  9. 设置电子邮件发件人:转到Jenkins - >管理Jenkins - >配置系统,然后按照下面的步骤操作。 enter image description here
  10. 最后,建立自己的詹金斯项目/任务:enter image description here
  11. 让我知道如果你还有任何问题;)
0

基础上贴出代码和您正在使用Maven的事实,我会说“MVN干净的测试”将运行所有的单元测试。因此,在Jenkins中创建meven项目(为此,您需要Jenkins中的maven插件),并在配置中提供“clean test”作为jenkins项目要执行的maven目标。

从这里看大图,虽然你的测试被注释为单元测试,但他们不具备单元测试的特征。首先,单元测试应该模拟外部依赖。

相关问题