FOR SETTING HUB
java -jar D:/SeleniumJar/selenium-server-standalone-3.4.0.jar -role hub
@echo off
cmd /k hub2.bat
FOR SETTING NODE
java -jar D:/SeleniumJar/selenium-server-standalone-3.4.0.jar -role node -hub http://192.168.1.4:4444/grid/register
@echo off
cmd /k node2.bat
(Pass IP address of Hub machine in node)
Make sure you have the latest version of driver(chrome/ie/ff) and selenium.
You can download from selenium official site
NOTE :http://localhost:4444/grid/console
After hitting in browser will display the linked hub and node
package com.selenium;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class Test123 {
WebDriver driver;
String baseUrl,nodeURL;
@Test
public void test() throws MalformedURLException{
baseUrl="http://facebook.com/";
nodeURL="http://192.168.1.4:4444/wd/hub";
DesiredCapabilities capability=DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
driver=new RemoteWebDriver(new URL(nodeURL),capability);
driver.manage().window().maximize();
driver.get(baseUrl);
}
}
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<!-- <configuration> <testExcludes> <exclude>**/search_orders_io_grid/*.java</exclude>
<exclude>**/insertion_orders/*.java</exclude> <exclude>**/sending_dfa/*.java</exclude>
<exclude>**/SHORT_US_SMOKE_BRIDGE.java</exclude> </testExcludes> </configuration> -->
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
--------------------------------------
/**
* Perform click action on element
*
* @param locator
* @return boolean
* @throws Exception
*/
public boolean click(WebElement element) throws Exception {
try {
waitForVisibilityOfElement(element);
waitForElementToBeClickable(element);
if(isWebElementEnabled(element)){
element.click();
return true;
}
else{
throw new TestFrameworkException("Click failed as the element is disabled.");
}
} catch(WebDriverException wde){
if(isWebElementEnabled(element)){
element.click();
return true;
}
} catch (Exception e) {
e.printStackTrace();
throw new TestFrameworkException("Failed to click, as either unable to locate element OR met an exception.", e);
}
return false;
}
==========
/**
* Waits for the visibility of the element (WebElement) on DOM of a page
*
* @param element
* @return WebElement
* @throws Exception
*/
public WebElement waitForVisibilityOfElement(WebElement element) throws Exception{
try {
return webdriverWait.until(ExpectedConditions
.visibilityOf(element));
} catch (Exception e) {
e.printStackTrace();
throw new TestFrameworkException("Failed in waitForVisibilityOfElement", e);
}
}
==========================
/**
* Wait for element to be visible and enabled such that you
* can click it.
*
* @param element
* @return WebElement
* @throws Exception
*/
public WebElement waitForElementToBeClickable(WebElement element) throws Exception {
return webdriverWait.until(ExpectedConditions
.elementToBeClickable(element));
}
==============================
/**
* First checks if element is displayed or not? If true then checks if it is enabled
*
* @param element
* @return True if the element is enabled, false otherwise.
* @throws Exception
*/
public boolean isWebElementEnabled(WebElement element) throws Exception{
if(isWebElementDisplayed(element)) {
if(!element.getAttribute("class").contains("disabled"))
return element.isEnabled();
}
return false;
}
===================
/**
* Is this element displayed or not? This method avoids the problem of having to parse an
* element's "style" attribute.
*
* @param element
* @return true if the element is displayed, false otherwise.
*
* @see http://stackoverflow.com/questions/24946703/selenium-webdriver-using-isdisplayed-in-if-statement-is-not-working
*/
public boolean isWebElementDisplayed(WebElement element) {
try{
if(element.isDisplayed())
return true;
}catch(Exception e){}
return false;
}
++++++++++++++++++
/**
* Clicks supplied WebElement (use this method when obvious ways to click using webdriver implementations fail)
* @param element
* @throws Exception
*/
public void JSClick(WebElement element) throws Exception {
try {
JavascriptExecutor js = (JavascriptExecutor) myDriver;
js.executeScript("arguments[0].click();", element);
} catch (Exception e) {
e.printStackTrace();
throw new TestFrameworkException("JSClick failed." + e);
}
}
java -jar D:/SeleniumJar/selenium-server-standalone-3.4.0.jar -role hub
@echo off
cmd /k hub2.bat
FOR SETTING NODE
java -jar D:/SeleniumJar/selenium-server-standalone-3.4.0.jar -role node -hub http://192.168.1.4:4444/grid/register
@echo off
cmd /k node2.bat
Make sure you have the latest version of driver(chrome/ie/ff) and selenium.
You can download from selenium official site
NOTE :http://localhost:4444/grid/console
After hitting in browser will display the linked hub and node
package com.selenium;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class Test123 {
WebDriver driver;
String baseUrl,nodeURL;
@Test
public void test() throws MalformedURLException{
baseUrl="http://facebook.com/";
nodeURL="http://192.168.1.4:4444/wd/hub";
DesiredCapabilities capability=DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
driver=new RemoteWebDriver(new URL(nodeURL),capability);
driver.manage().window().maximize();
driver.get(baseUrl);
}
}
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<!-- <configuration> <testExcludes> <exclude>**/search_orders_io_grid/*.java</exclude>
<exclude>**/insertion_orders/*.java</exclude> <exclude>**/sending_dfa/*.java</exclude>
<exclude>**/SHORT_US_SMOKE_BRIDGE.java</exclude> </testExcludes> </configuration> -->
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
--------------------------------------
/**
* Perform click action on element
*
* @param locator
* @return boolean
* @throws Exception
*/
public boolean click(WebElement element) throws Exception {
try {
waitForVisibilityOfElement(element);
waitForElementToBeClickable(element);
if(isWebElementEnabled(element)){
element.click();
return true;
}
else{
throw new TestFrameworkException("Click failed as the element is disabled.");
}
} catch(WebDriverException wde){
if(isWebElementEnabled(element)){
element.click();
return true;
}
} catch (Exception e) {
e.printStackTrace();
throw new TestFrameworkException("Failed to click, as either unable to locate element OR met an exception.", e);
}
return false;
}
==========
/**
* Waits for the visibility of the element (WebElement) on DOM of a page
*
* @param element
* @return WebElement
* @throws Exception
*/
public WebElement waitForVisibilityOfElement(WebElement element) throws Exception{
try {
return webdriverWait.until(ExpectedConditions
.visibilityOf(element));
} catch (Exception e) {
e.printStackTrace();
throw new TestFrameworkException("Failed in waitForVisibilityOfElement", e);
}
}
==========================
/**
* Wait for element to be visible and enabled such that you
* can click it.
*
* @param element
* @return WebElement
* @throws Exception
*/
public WebElement waitForElementToBeClickable(WebElement element) throws Exception {
return webdriverWait.until(ExpectedConditions
.elementToBeClickable(element));
}
==============================
/**
* First checks if element is displayed or not? If true then checks if it is enabled
*
* @param element
* @return True if the element is enabled, false otherwise.
* @throws Exception
*/
public boolean isWebElementEnabled(WebElement element) throws Exception{
if(isWebElementDisplayed(element)) {
if(!element.getAttribute("class").contains("disabled"))
return element.isEnabled();
}
return false;
}
===================
/**
* Is this element displayed or not? This method avoids the problem of having to parse an
* element's "style" attribute.
*
* @param element
* @return true if the element is displayed, false otherwise.
*
* @see http://stackoverflow.com/questions/24946703/selenium-webdriver-using-isdisplayed-in-if-statement-is-not-working
*/
public boolean isWebElementDisplayed(WebElement element) {
try{
if(element.isDisplayed())
return true;
}catch(Exception e){}
return false;
}
++++++++++++++++++
/**
* Clicks supplied WebElement (use this method when obvious ways to click using webdriver implementations fail)
* @param element
* @throws Exception
*/
public void JSClick(WebElement element) throws Exception {
try {
JavascriptExecutor js = (JavascriptExecutor) myDriver;
js.executeScript("arguments[0].click();", element);
} catch (Exception e) {
e.printStackTrace();
throw new TestFrameworkException("JSClick failed." + e);
}
}
No comments:
Post a Comment