在eclipse中创建了一个java测试脚本,如何在无头的Linux CentOS VM中运行它?

时间:2013-08-19 13:29:06

标签: java selenium centos selenium-webdriver

上面的问题涵盖了我需要的东西我在Windows上使用eclipse创建了成功的测试脚本,但是现在我需要能够通过SSH在linux vm中运行它并且我在线尝试了很多东西而没有任何成功我知道可能还需要对代码进行一些更改,但我仍然成功地做到了这一点,所以请看下面的代码。

我已经将src(测试脚本)和libs(jars)文件从eclipse工作区复制到了linux vm,并且理解为了执行selenium独立服务器我使用的是java -jar,但这是关于它的。如果你能指导我完成在vm中对firefox进行测试所需采取的步骤。

我安装了java和firefox。

测试脚本

package test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class CorrectTestBackLink {
public BrowserList.DriverToUse used_driver = BrowserList.DriverToUse.IEXPLORER; 
private BrowserSelector selectedDriver = new BrowserSelector(used_driver);
private WebDriver driver = selectedDriver.getDriver();
private StringBuffer verificationErrors = new StringBuffer();
private String baseUrl;
private static String resulterValue = "110";
private String valuecheck1;



@Before 
public void setUp() throws Exception {
    baseUrl = "http://url/";    
            //Set the column number and datatype of column by using either StringColumnNumber"Letter" or IntColumnNumber"Letter.
    // Add aditional columns to database class where necessary.

}
public void calcResultChecker(){
    driver.findElement(By.id("resulter")).getAttribute("value");
    String valuecheck1 = driver.findElement(By.id("resulter")).getAttribute("value");
    if (valuecheck1.equals(resulterValue)){
        System.out.println("The Resulting value ("+valuecheck1+")  is CORRRECT!");          
    }else{
        System.err.println("The Resulting value is ("+valuecheck1+") is INCORRRECT!");  

    }
}
@Test
public void test2() throws Exception {
    //calc page
    driver.get(baseUrl + "calc.php");
    assertEquals(baseUrl + "calc.php", driver.getCurrentUrl()); 
    System.out.println("We are on the correct page ("+driver.getCurrentUrl()+").");
    Thread.sleep(200);
    System.out.println("Entering 10 into 'firstnumber' field!");
    driver.findElement(By.name("firstnumber")).sendKeys("10");
    Thread.sleep(200);
    driver.findElement(By.name("secondnumber")).sendKeys("11");
    System.out.println("Entering 11 into 'secondnumber' field!");
    Thread.sleep(200);
    System.out.println("Clicking calculate button!");
    driver.findElement(By.name("Calculate")).click();
    Thread.sleep(200);

    //calc results page
    assertEquals(baseUrl + "calcresult.php", driver.getCurrentUrl());   
    System.out.println("We are on the correct page ("+driver.getCurrentUrl()+").");
    valuecheck1 = driver.findElement(By.id("resulter")).getAttribute("value");
    assertEquals(valuecheck1, resulterValue);       
    Thread.sleep(200);
    System.out.println("Clicking back Link!");
    driver.findElement(By.linkText("Back")).click();
    Thread.sleep(200);

    //calc page check
    assertEquals(baseUrl + "calc.php", driver.getCurrentUrl()); 
    System.out.println("We are on the correct page ("+driver.getCurrentUrl()+").");
    Thread.sleep(200);
    System.out.println("Test Complete");
    Thread.sleep(200);
    driver.quit();
}

@After

public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}
}

浏览器选择器

package test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;


 public class BrowserSelector {
private WebDriver driver;
public BrowserSelector(BrowserList.DriverToUse used_driver){
    switch (used_driver){
    case CHROME:{
        System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
        driver = new ChromeDriver();
        break;
    }
    case FIREFOX:{
        driver = new FirefoxDriver();
        break;
    }   
    case IEXPLORER:{
        System.setProperty("webdriver.ie.driver","C:/path/IEDriverServer.exe"); 
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        driver = new InternetExplorerDriver(capabilities);

        break;
    } 
    }
}

public WebDriver getDriver(){
    return driver;
}
}

1 个答案:

答案 0 :(得分:1)

人们通常使用PhantomJS或“Headless Chrome”在无头环境中运行。此外,有些人从XVFB帧缓冲区运行常规浏览器。我发现Here is a blog article解释了如何安装XVFB。

xvfb-run --server-args='-screen 0, 1024x768x16' google-chrome 
    -start-maximized http://example.com > /dev/null &

使用XVFB和VNC,您可以远程登录并观看您的测试在无头盒上运行,但我还没试过。你必须试验它。