Hi,
I am very new to Selenium and am trying to use it for cross browser
testing. I am facing issues when I try to run same test in parallel on
multiple browsers.
I am using testng with webdriver(2.20 version).
FF 4.0 IE9
I have an xml file like this :
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Same TestCases on Different Browsers and ports"
verbose="3" parallel="tests" thread-count="2">
<test name="Run on Firefox">
<parameter name="browser" value="firefox"/>
<parameter name="username" value="xxxx"/>
<parameter name="password" value="xxxx"/>
<parameter name="port" value="5555"/>
<parameter name="path" value="path to ff profile"/>
<classes>
<class name="com.WebUploadsFFIE"/>
</classes>
</test>
<test name="Run on Internet Explore port 5556">
<parameter name="browser" value="iexplore"/>
<parameter name="username" value="xxx"/>
<parameter name="password" value="xxx"/>
<parameter name="port" value="5556"/>
<parameter name="path" value=""/>
<classes>
<class name="com.WebUploadsFFIE"/>
</classes>
</test>
</suite>
I have a baseclass whihc starts the selenium server.
I have a before test annotation where i am setting capabilities for FF/
IE.
If i run a single test either on IE or FF it passes. But when I am
trying to run them parallely i am facing issues.
Base class for starting Selenium Server :
package com;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
//import org.testng.annotations.Parameters;
import java.net.BindException;
import java.net.ServerSocket;
public class StartSeleniumServer {
public static SeleniumServer server;
//@Parameters({"server_port"})
@BeforeSuite(alwaysRun = true)
public static void startSeleniumServer() throws Exception {
try {
ServerSocket serverSocket = new
ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
serverSocket.close();
//Server not up, start it
try {
RemoteControlConfiguration rcc = new
RemoteControlConfiguration();
rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
server = new SeleniumServer(false, rcc);
} catch (Exception e) {
System.err.println("Could not create Selenium
Server because of: "
+ e.getMessage());
e.printStackTrace();
}
try {
server.start();
System.out.println("Server started");
} catch (Exception e) {
System.err.println("Could not start Selenium
Server because of: "
+ e.getMessage());
e.printStackTrace();
}
} catch (BindException e) {
System.out.println("Selenium server already up, will
reuse...");
}
}
@AfterSuite(alwaysRun = true)
public static void stopSeleniumServer(){
if (server != null)
{
try
{
server.stop();
server = null;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
My Actual test class:
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;
import static org.junit.Assert.*;
public class WebUploadsFFIE extends StartSeleniumServer {
public String username;
public String password;
public static WebDriver driver;
private static Properties properties;
@Parameters({"browser","username","password","port","path"})
@BeforeTest
public void setup(String browser, String username, String password,
String port, String path) throws IOException {
properties = new Properties();
properties.load(new FileInputStream("verizon_test.properties"));
assertNotNull("Invalid Portal URL",
properties.getProperty("Portal.url"));
String PROXY = "
cache2.lexmark.com:8080";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities capability=null;
this.username = username;
this.password = password;
if(browser.equalsIgnoreCase("iexplore")){
System.out.println("iexplore");
capability=DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
capability.setCapability(CapabilityType.PROXY, proxy);
capability.setCapability("port", port);
}
if(browser.equalsIgnoreCase("firefox")){
FirefoxProfile profile = new FirefoxProfile(new File(path));
profile.setEnableNativeEvents(false);
System.out.println("firefox");
capability= DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
capability.setCapability(CapabilityType.PROXY, proxy);
capability.setCapability(FirefoxDriver.PROFILE, profile);
capability.setCapability("port", port);
}
driver = new RemoteWebDriver(new URL("
http://localhost:4444/wd/
hub"), capability);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.navigate().to(properties.getProperty("Portal.url"));
}
@Test
public void test_txt_file() throws Exception {
driver.findElement(By.id("IDToken1")).sendKeys(username);
driver.findElement(By.id("IDToken2")).sendKeys(password);
driver.findElement(By.name("signin")).click();
try {
assertTrue(Wait_For_String("My Print Queue"));
Thread.sleep(200);
}catch (Throwable t) {
System.out.println("Error in file uplaod");
}
WebElement inputElement = driver.findElement(By.className("x-btn-
inner"));
((JavascriptExecutor)driver).executeScript("arguments[0].checked =
true;", inputElement);
inputElement.click();
try {
assertTrue(Wait_For_String("Done"));
Thread.sleep(200);
}catch (Throwable t) {
System.out.println("Error in file uplaod");
}
inputElement = driver.findElement(By.name("file"));
((JavascriptExecutor)driver).executeScript("arguments[0].checked =
true;", inputElement);
driver.findElement(By.name("file")).sendKeys("C:\\Testing_Originals\
\ODS-testfile1.ods");
driver.findElement(By.name("file")).sendKeys("C:\\Testing_Originals\
\html-file1.html");
driver.findElement(By.name("file")).sendKeys("C:\\Testing_Originals\
\ODP-test_file1.odp");
driver.findElement(By.name("file")).sendKeys("C:\\Testing_Originals\
\print-stream-pg-count-test-1.JPG");
assertTrue(Wait_For_String("Done"));
driver.findElement(By.xpath("//span[.='Done']")).click();
//driver.switchTo().defaultContent();
assertTrue(Wait_For_String("My Print Queue"));
Thread.sleep(200);
inputElement = driver.findElement(By.className("sign-in-text-
name"));
((JavascriptExecutor)driver).executeScript("arguments[0].checked =
true;", inputElement);
inputElement.click();
inputElement = driver.findElement(By.xpath("//span[.='Sign Out']"));
((JavascriptExecutor)driver).executeScript("arguments[0].checked =
true;", inputElement);
inputElement.click();
}
private static boolean Wait_For_String(String Message) {
int timeout = 0;
do{
boolean result = driver.getPageSource().contains(Message);
if((result == false) && (timeout > 300)) { return result; }
timeout++;
} while(timeout <= 300 );
return true;
}
@AfterTest
public void tearDown(){
System.out.println("Closing the driver==========\n===============
\n==========\n================\n==============");
try {
driver.quit();
}catch(Exception e) {
System.out.println("Driver quit failed with error" + e);
}
}
}
-------------------
On execution the test starts on IE & ff but fails either with
"Unreachable browser" exception or
"Error 500 java.util.concurrent.RejectedExecution" etc.
I am not sure where I am going wrong. Any help would be apprciated.
Thanks
Bhoomika