How to set up Grid 2 in selenium RC using through Java-TestNG - selenium-grid

I am new to Grid and am trying to set up Grid 2 in Selenium RC.
I have downloaded the selenium-server-standalone-2.1.0.jar. Also I have my TestNG test cases.
And I have a test XML like:
<suite thread-count="1" name="Suite" parallel="tests" verbose="10"><!-- tests -->
<test name="FFTest" preserve-order="true">
<parameter name="selenium.host" value="localhost"></parameter>
<parameter name="selenium.port" value="5569"></parameter>
<parameter name="selenium.browser" value="*firefox"></parameter>
<parameter name="selenium.url" value="http://www.google.com"></parameter>
<classes>
<class name="EmployeeHealth.TestScripts.EmployeeHealthRegressionSuite">
<methods>
<include name="InitScript"></include>
<include name="SelectAvoidableAdmissionModule"></include>
</methods>
</class>
</classes>
</test>
<test name="IETest" preserve-order="true">
<parameter name="selenium.host" value="localhost"></parameter>
<parameter name="selenium.port" value="5579"></parameter>
<parameter name="selenium.browser" value="*iehta"></parameter>
<parameter name="selenium.url" value="http://www.google.com"></parameter>
<classes>
<class name="EmployeeHealth.TestScripts.EmployeeHealthRegressionSuite">
<methods>
<include name="InitScript"></include>
<include name="SelectAvoidableAdmissionModule"></include>
</methods>
</class>
</classes>
</test>
</suite>
And I followed the below steps:
Start Hub:
java -jar selenium-server-standalone-2.5.0.jar -role hub
Start remote control supporting Firefox
java -jar selenium-server-standalone-2.5.0.jar -role RC -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS -port 5579
Start another RC supporting Internet explore
java -jar selenium-server-standalone-2.5.0.jar -role RC -hub http://localhost:4444/grid/register -browser browserName=iexplore,platform=WINDOWS -port 5556.
And in my SetUp.java file, Am creating the selenium objects like:
selenium = new DefaultSelenium(host,Integer.parseInt(port),
browser, url);
if ( browser.equalsIgnoreCase("*chrome")) {
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setFirefoxProfileTemplate(new File(
"test\\Resources\\ThirdParty\\FirefoxProfile\\"));
seleniumServer = new SeleniumServer(rcc);
} else {
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setFirefoxProfileTemplate(new File("Object Repository\\SSL"));
//"src\\Script\\lib\\ThirdParty\\FirefoxProfile\\"));C:\\Biju\\NewFrameworkStrcuture\\Framework\\ABC_JSAF\\
seleniumServer = new SeleniumServer(rcc);
RemoteControlConfiguration a= seleniumServer.getConfiguration();
File uimap=new File("src\\Script\\lib\\user-extensions.js");
a.setUserExtensions(uimap);
//seleniumServer = new SeleniumServer();
}
//seleniumServer.start();
selenium.start();
selenium.windowMaximize();
selenium.windowFocus();
By this way, when am running the xml file, the tests are running in sequence(First Firefox and then the IE). But not in parallel.
Also i tried giving the #Test parameter like
#Test(dataProvider="CommonTestData",threadPoolSize = 3)
But i dont see any change in the seqence.
do any one got why this is happening?..
Appreciate for your help.
A J, Thanks for the reply.
I did changes in the XML suite and I made the dataprovider function as "parallel=true" and also added the Invokation function #Test parameter "InvoationCount=2",and configure the node and am able to launch 1 IE and 1 FF parallel.
But now the problem is, in one browser(Say IE), my test cases are executing perfectly.But in the other browser(FF) ,after launching the browser, I don't see any other action performing on it.Not even opening my URL. in the console I got error :
"FAILED CONFIGURATION: #BeforeClass Seleniumstart
("localhost", "5569", "*firefox", "http://www.google.com")
com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be
null; has this session been started yet?"
Any idea on this?..

If you are using dataprovider, you also need to mention whether it should be called in parallel. Your CommonTestData method should have parallel=true option.
For executing using the XML, you need to increase the thread count to more than 1 for parallel execution.

It looks like your Firefox XML config tries to send the test to a node on port 5569. But in your actual client node connection string you are assigning it to use port 5579. It also looks like your IE settings attempt to use 5579 but are started with 5556.

Related

Is it posible to run feature cucumber in parallel in different browser

I'm working in a big project, i want to run eature cucumber in parallel in different browser
I have the featuren the step definition ? the webdriverfactory and the shared preferences.
I have this method in webfactory and it works and i write the testng.xml
public WebDriver driver;
public static WebDriver get() {
WebDriver driver = null ;
System.setProperty("webdriver.chrome.driver","D:\\Drive\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
return(driver);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteSopraHR" parallel="tests">
<test name="testie">
<!-- <parameter name="myBrowser" value="ie" /> -->
<classes>
<class name="com.driver.WebDriverFactory"/>
</classes>
</test> <!-- Test -->
<test name="testchrome">
<!-- <parameter name="myBrowser" value="chrome" /> -->
<classes>
<class name="com.driver.WebDriverFactory"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
I don't know how to change the other method because it didn't has any parameter to pass and it return a web driver.
when I have changed all the other method in other classes have a problem with it
any suggestion please.
and is the cucumber-jvm can run feature in parallel in different browser ??? or in console ???
You can indeed run Cucumber features and scenarios in parallel using Courgette-JVM
When you run your tests, you can set a System property that would target the browser you wish to use in parallel.
Another useful library to manage your driver binaries is WebDriver Binary Downloader
You can then specify the browser to use at runtime using:
System.setProperty("browser", "chrome");
or
VM option -Dbrowser="chrome"
private WebDriver driver;
public void createDriver() {
final String browser = System.getProperty("browser", "chrome").toLowerCase();
switch (browser) {
case "chrome":
WebDriverBinaryDownloader.create().downloadLatestBinaryAndConfigure(BrowserType.CHROME);
driver = new ChromeDriver();
case "firefox":
WebDriverBinaryDownloader.create().downloadLatestBinaryAndConfigure(BrowserType.FIREFOX);
driver = new FirefoxDriver();
default:
throw new RuntimeException("Invalid browser specified!");
}
}
We are using QAF-Gherkin-client, where you can configure it using one or more xml test nodes. You can run scenarios in parallel as well. You don't need to write any code for driver management or other functional testing common needs.
<suite name="AUT Test Automation" verbose="0" parallel="methods">
<test name="Tests on chrome">
<parameter name="driver.name" value="chromeDriver"/>
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
<test name="Tests FF">
<parameter name="driver.name" value="firefoxDriver"/>
<classes>
<class name="com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory" />
</classes>
</test>
</suite>
I think, you need to add switch construction in your method and parameter - type of browser from testng.xml.
Also, as I know, parallel execution will work only with a non-static Driver.

Is the parallel execution possible, without selenium grid?or (only testNG is enough?)

I am beginner in selenium webdriver. so for parallel execution normally we do changes in xml file like parallel="methods" thread-count="3" and my doubt is:
Is the parallel execution possible without selenium grid?
or only testNG is enough?
Yes, you may run UI tests in parallel without grid, or using only selenium grid node directly, without hub. Each thread in TestNG will open additional browser window, but you will get unpredictable issues in case when you application will manage all connections from you host as one user session.
Yes, using #Parameters ("browser") of TestNg..sample code as below...
#Parameters ("browser")
public void test(String browserName) {
if(browserName.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
} else if (browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe" );
driver = new ChromeDriver();
}
}
write your test after that in your testng.xml use parallel option also mention the parameter value.. sample code as below..
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite789787" parallel="tests">
<test name="FFTest98798">
<parameter name="browser" value="firefox"/>
<classes>
<class name ="crossbrowsertest.VerifyTitle" >
</class>
</classes>
</test> <!-- Test -->
<test name="ChromeTest8999">
<parameter name="browser" value="chrome"/>
<classes>
<class name ="crossbrowsertest.VerifyTitle" >
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
hope this helps

TestNG Parameter behaving inconsistently when executed multiple time

I want to run multi-browser testing. For that, this is my testng.xml
<suite name="MultiBrowsreTest" parallel="tests">
<test name="T1" >
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.core.My"/>
</classes>
</test>
<test name="T2" >
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.core.My"/>
</classes>
</test>
</suite>
And Here is my Java program.
public class My {
HH h ;
#Test
#Parameters("browser")
public void my1(String browser){
h = new HH();
h.browser = browser;
System.out.println("Browser: "+h.browser);
}
}
When I run the program,it gives me different output all the time. i.e.
1:
Browser: firefox
Browser: firefox
2:
Browser: chrome
Browser: firefox
3:
Browser: chrome
Browser: chrome
4:
Browser: firefox
Browser: chrome
Can someone please suggest me the solution so that I will get consistent result
parallel=true executes all tests at a time(in parallel) and hence whichever test it gets first is executed first.So, test order is not maintained and hence different outputs every time.
Make parallel = none, and it will follow the order as mentioned in testng xml.
<suite name="MultiBrowsreTest" parallel="none">

How to run single selenium test case in two different nodes?

I am new to selenium,I need to run my single selenium test case in two different nodes using FIREFOX browser (selenium grid),I have started my hub using below command
java -jar selenium-server-standalone-2.32.0.jar -role hub
Node 1:
java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 9595
Node 2:
java -jar selenium-server-standalone-2.32.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 8585
two nodes has been created to hub .But when i run a testcase in hub,only one node is executing the test case and the other node remain available but not executing the testcase .
2) The node is selected randomly by hub while executing the testcase.
My question:
Run testcase in both the nodes simultaneously
Nodes can be declared as follow:-
Node 1 for chrome java -Dwebdriver.chrome.driver=C:\drivers\chromedriver.exe -jar selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register -port 8585 -browserName=chrome
Node 2 for firefox: java -jar selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register -port 9595 -browserName=firefox
You need to have following testng.xml to run same test case on different browsers:-
<suite name="Selenium TestNG Suite" parallel="tests" thread-count="5">
<test name="Selenium TestNG - 1">
<parameter name="browser" value="firefox" />
<parameter name="port" value="9595" />
<classes>
<class name="grid.testcase" />
</classes>
</test>
<test name="Selenium TestNG - 2">
<parameter name="browser" value="chrome" />
<parameter name="port" value="8585" />
<classes>
<class name="grid.testcase" />
</classes>
</test>
</suite>
In your test case, write code something like this:-
package grid;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class testcase {
public WebDriver driver=null;
#Parameters({"browser","port"})
#BeforeClass
public void initialize(String browser, String port) throws MalformedURLException{
DesiredCapabilities capability= new DesiredCapabilities();
capability.setBrowserName(browser);
driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
}
#Test
public void testThis() throws InterruptedException {
String url="https://www.google.com";
driver.get(url);
driver.manage().window().maximize();
//do something
driver.close();
}
}
I am not sure what framework you are using. I did parallel execution using "testng" framework.
For using testng you have to add testng jar file in your project build path or if you used IDE like eclipse add testng plugin.
Then create your class with proper annotation(ie. #test).
Create testNG.xml file and configure the things.(make sure You mentioned parallel=2. the count 2 is only how many test you want to execute in parallel)
Then right click the xml file and click runas testNG..
Refer :
testng-executing-parallel-tests
parallel-execution-of-test-methods-in-testng

Selenium grid runs one node at a time

When does Selenium hub run one node at a time? I might be wrong around both selenium code and testNG xml.
I've configured TestNG.xml to run parallel threads and grid framework seems alright, hub active on 5555, http://xx.xx.xx.xx:5555/grid/console shows two nodes connected and active with IE browser. IE driver server is used to launch IE on both these nodes, selenium-server-standalone-2.35.0 is used for the grid on all nodes and hub. When I 'run as TestNG Test' on eclipse or through Jenkins via pom.xml, the script launches on node1 first and in the next run launches on node 2.
Could it be: 1. Wrong testNG.xml/selenium grid code? 2. Hub starts up with maxinstances=1, is this causing it? What's the solution to it? Tried using hubconfig.json but doesn't seem to take affect. 3. Any misconfiguration at nodes? 4. IEDriverServer or selenium server version issues? [tried 37/39 versions as well]..
TestNG.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="CO" verbose='1' parallel="tests" thread-count="10" preserve-order="true">
<test name="FI" preserve-order="true">
<parameter name="browser" value="internet explorer" />
<parameter name="port" value="5566" />
<classes>
<class name="src/test/java.clickonce.remoteFresh"/>
</classes>
</test> <!-- Test -->
<test name="Ad" preserve-order="true">
<parameter name="browser" value="internet explorer" />
<parameter name="port" value="5567" />
<classes>
<class name="src/test/java.clickonce.Admin"/>
</classes>
</test>
</suite>
<!-- Suite -->
Selenium code for grid:
#BeforeTest
public void setUp() throws IOException {
baseUrl = "http://xxx/";
nodeUrl = "http://xx.xx.xx.xx/wd/hub";
String sUrl = "http://xxx";
DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplorer");
capability.setPlatform(Platform.WINDOWS);
capability.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true );
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
File file = new File("d:/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver WebDriverObj = new InternetExplorerDriver();
WebDriverObj.get(sUrl);
driver = new RemoteWebDriver(new URL(nodeUrl), capability);
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}
Node commands -
java -jar selenium-server-standalone-2.35.0.jar - Dwebdriver.ie.driver="D:\IEDriverServer.exe" -role webdriver -hub http://
xx.xx.xx.xx:5555/grid/register -port 5566 -browser "browserName=iexplorer,platform=WINDOWS"
node 2 on 5567 port
Instead of using -role webdriver on the Node command, use -role node. Also, change the way you specify the hub and its port: change -hub http://xx.xx.xx.xx:5555/grid/register -port 5566 by -hub http://xx.xx.xx.xx/grid/register -port 5555. In the node command, you have to use the port where the hub is listening.
So, the final version of the node command will be:
java -jar selenium-server-standalone-2.35.0.jar -role node -hub http://xx.xx.xx.xx/grid/register -port 5555 -Dwebdriver.ie.driver="D:\IEDriverServer.exe" -browser "browserName=iexplorer,platform=WINDOWS"
Check what the user-data-dir switch is set to in the RemoteWebDriver arguments. If every time you launch you write to the same profile, you won't be able to launch 2 nodes at the same time.
public ChromeUserImpl(URL url) {
DesiredCapabilities cap = DesiredCapabilities.chrome();
ChromeOptions o = new ChromeOptions();
o.addArguments("user-data-dir=" <SHOULD BE DIFFERENT IN ORDER TO RUN SIMULATANOUSLY>);
o.addArguments("test-type");
o.addArguments("--start-maximized");
cap.setCapability(ChromeOptions.CAPABILITY, o);
mDriver = new RemoteWebDriver(url, cap);
}