0

上面的问题涵盖了我需要什么我已经在 windows 上使用 eclipse 创建了成功的测试脚本,但是现在我需要能够通过 SSH 在 linux vm 中运行它,并且我在网上尝试了很多东西但没有任何成功我知道一些变化可能还需要对代码进行处理,但我也没有成功地做到这一点,所以请参阅下面的代码。

我已经将 src(test scripts) 和 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;
}
}
4

1 回答 1

1

人们通常使用 PhantomJS 或“Headless Chrome”在无头环境中运行。此外,有些人从 XVFB 帧缓冲区运行常规浏览器。这是我发现的一篇博客文章,它解释了如何安装 XVFB。

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

使用 XVFB 和 VNC,您可能可以远程登录并观看您的测试在无头盒子上运行,但我还没有尝试过。你将不得不尝试它。

于 2014-02-26T16:00:58.183 回答