简体   繁体   中英

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.

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. If you could walk me through the steps that need to be taken to run a test on firefox in the vm.

I have java and firefox installed.

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. Also, some people run regular browsers from a XVFB framebuffer. Here is a blog article that I found that explains how to install 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. You would have to experiment with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM