[英]Created a java test script in eclipse, how do i run it in a headless linux CentOS VM?
The question above covers what I need I have created successful test scripts using eclipse on windows, but now i need to be able to run it in a linux vm through SSH and I have tried many things online and not had any success I know some changes may need to be done to the code also but I havnt managed to do that successfully either so please see the code below. 上面的问题涵盖了我在Windows上使用eclipse创建成功的测试脚本所需要的东西,但是现在我需要能够通过SSH在linux vm中运行它,并且我已经尝试了许多在线操作,但是没有任何成功,我知道一些更改可能还需要对代码进行处理,但是我还是设法成功完成了该操作,因此请参见下面的代码。
I have copied the src(test scripts) and libs(jars) files from the eclipse workspace to the linux vm and understood that to execute the selenium standalone server i use java -jar but that is about it. 我已经将ecrcse工作空间中的src(测试脚本)和libs(jars)文件复制到了linux vm上,并了解到要执行selenium独立服务器,我使用java -jar,但仅此而已。 If you could walk me through the steps that need to be taken to run a test on firefox in the vm. 如果您可以指导我完成在vm中对Firefox进行测试所需的步骤。
I have java and firefox installed. 我已经安装了java和firefox。
Test Script 测试脚本
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);
}
}
}
Browser Selector 浏览器选择器
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;
}
}
People usually use either PhantomJS or "Headless Chrome" to run in a headless environment. 人们通常使用PhantomJS或“ Headless Chrome”在无头环境中运行。 Also, some people run regular browsers from a XVFB framebuffer. 另外,有些人从XVFB帧缓冲区运行常规浏览器。 Here is a blog article that I found that explains how to install XVFB. 这是我发现的博客文章 ,它解释了如何安装XVFB。
xvfb-run --server-args='-screen 0, 1024x768x16' google-chrome
-start-maximized http://example.com > /dev/null &
Using XVFB and VNC, you could probably login remotely and watch your tests run on the headless box but I haven't tried this. 使用XVFB和VNC,您可能可以远程登录并观看测试在无头盒上运行,但是我还没有尝试过。 You would have to experiment with it. 您将不得不尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.