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

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

Related

Unknown Option: -protocol error while using selenium webdriver

I get the below exception while trying to execute testng.xml using chromedriver
Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -protocol
I have chromedriver.exe in my local and works fine for non-testng tests. Has someone faced a similar issue.
This is nowhere related to the Chromedriver. It is the issue with the parameter named protocol` that you are not passing to the test method through the testng.xml file. For ex.
#Parameter("protocol")
#Test
void sampleTest(String protocol){
//your code
}
Expected testng.xml file
<suite name="Sample Suite">
<test name="Sample Test">
<parameter name="protocol" value="expectedvalue">
</test> <!-- test -->
</suite>
Some time to avoid this situation, we use #Optional attribute
#Parameter("protocol")
#Test
void sampleTest(#Optional("http")String protocol){
//your code
}

Not able to run TestNG Suite (Selenium Webdriver)

I am new to TestNg , Selenium.
My problem is I am not able to run my TestNG suite. When I click my testNg suite (By right clicking crossBrowser.xml and click TestNG Suite), It is not running, It does not show any error in the console.
Please find the code I am using.
VerifyTitle.java
package testNGTestPack;
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.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class VerifyTitle {
WebDriver driver;
#Test
#Parameters("browserName")
public void verifyTitle(String browserName){
if(browserName.equalsIgnoreCase("firebox")) {
driver=new FirefoxDriver(); }
else if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\driver\\IEDriverServer.exe");
driver=new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
//driver.quit();
}
}
crossBrowser.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testNGTestPack">
<test name="Test">
<parameter name="browserName" value="firebox"/>
<classes>
<class name="testNGTestPack.VerifyTitle"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Eclipse Version : : Luna Service Release 2 (4.4.2)
When right clicking against crossBrowser.xml which is showing Run As TestNg Suite. But clicking on it, it does not trigger any action. Even in console, it show any error.
Do I need to include any jar file? Please give me a solution
Yes you need to create a new configuration for this.
Select your testng.xml file > Run as > Run Configuration > Click on TestNG on left panel > Right click and and CLick on new > Click on Suite > Browse xml and click on run.
Please try and let me know.
Thanks
Mukesh

TestNG configuration failure

I am trying to run a simple TestNG test from the command line, it errors with a configuration failure. The same TestNG test will run from Eclipse IDE correctly. From command line it does not work. This is a severe limitation of this TestNG framework making it not very useable in a Continuous integration theme. If you plan on working with TestNG ensure you can get it running from the command line before committing to using it as your testing framework. Having to run it from Eclipse IDE is a severe limitation.
TestSuite_Bollosk
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
command line syntax is:
java -cp "C:\correctclpath1\*;C:\correctclpath2\*" org.testng.TestNG "C:\anotherpath\TS_simpletest.xml"
the test looks like this:
package pkgTSBollosk;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TC_Bollosk
{
WebDriver driver;
#Parameters({"param1","param2"})
#Test
public void bollosktestmethod(String param1, String param2) throws InterruptedException
{
assert(true);
System.out.println("test output for bollosk test:");
}
#BeforeTest
public void beforeTest() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
}
#AfterMethod
public void afterTest() {
driver.quit();
driver = null;
}
}
the TS_simpletest.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Bollosk Test Suit" parallel="false">
<test name="Bolluks test name">
<parameter name="Param1" value="not - used"></parameter>
<parameter name="Param2" value="not - used"></parameter>
<classes>
<class name="pkgTSBollosk.TC_Bollosk">
<methods>
<include name = "bollosktestmethod"></include>
</methods>
</class>
</classes>
</test>
</suite>
Substituting Assert.assertTrue for assert and importing org.testNG.assert fixed the problem. A trap for the newbie..... I retract my previous statement about severe limitation.
public class test1 {
public WebDriver d;
#Parameters({"browsername","url"})
#BeforeMethod
public void browserselections(String broname,String URL) {
System.out.println(broname);
System.out.println(URL);
if (broname.equalsIgnoreCase("ff")) {
d = new FirefoxDriver();
} else if (broname.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "F:\\All jars\\chromedriver.exe");
d = new ChromeDriver();
}
d.manage().window().maximize();
d.get(URL);
}
#Test
public void tc1() {
d.findElement(By.id("lst-ib")).sendKeys("testing");
}
}
below is my xml code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="skip">
<test name="Test1">
<parameter name="browsername" value="ff"/>
<parameter name="URL" value="https://www.google.com"/>
<classes>
<class name="testng.test1" />
</classes>
</test>
<test name="Test2">
<parameter name="browsername" value="chrome"></parameter>
<classes>
<class name="testng.test1" />
</classes>
</test>
</suite> <!-- Suite -->enter code here
I was facing the same issue and i noticed that all of my jar files were in different sub folders. Move all of them in one library folder and execute with same command.
java -cp "%projectLocation%/Library/*";"%projectLocation%/target/classes/" org.testng.TestNG testng.xml
Try TestNG config policy. Add parameter configfailurepolicy="continue" if you wish to proceed with config failures else, configfailurepolicy="skip". Example
<suite name="Bollosk Test Suit" configfailurepolicy="continue">
If you remove driver.quit() from public void afterTest() method,
the Configuration Failures will not come.

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);
}

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

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.