Cross Browser testing in TestNG with selected test cases - selenium-webdriver

Is it possible to run cross browser testing from TestNG with selected test cases on selected browser? For example. 1 - 5 test cases on IE and 6-10 on chrome etc.
Thanks,
Sudhakar

Yes, this is possible
Let me show you how i have done this
Into TestNG suite file i will give browser name as parameter
Now add the code to fetch browser name in beforeTest method
#BeforeTest(alwaysRun = true)
public void fetchSuiteConfiguration(ITestContext testContext) {
targetBrowser=testContext.getCurrentXmlTest().getParameter("selenium.browser");}
Now initialize browser into beforeMethod
#BeforeMethod(alwaysRun = true)
public void setUp(Method method, ITestContext testContext) {
if (targetBrowser == null || targetBrowser.contains("firefox")) {
/*initialize firefox driver here*/
} else if (targetBrowser.contains("ie")) {
/*initialize ie driver here*/
} else if (targetBrowser.contains("opera")) {
/*initialize opera driver here*/
} else if (targetBrowser.contains("chrome")) {
/*initialize Chrome driver here*/
}
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(url);
driver.manage().window().maximize();
}

Related

Unable to Zoom Out of screen using Selenium WebDriver

Am trying to zoom out of my current page as some of the monitors are quite small there our application is not loading properly.
Selenium 3.141.59
Java 1.8
This is my sample code:
public class ScreenResolutionCheck {
static WebDriver driver = null;
public static void main(String[] args) {
int width = 1512, height = 982; //Default Macbook Pro Size
WebDriverManager webDriverManager = WebDriverManager.chromedriver().browserInDocker().enableVnc().linux().dockerScreenResolution(width + "x" + height +"x24").timeout(100);
driver = webDriverManager.create();
BaseFunctions.logInfoWithOutScreenShot("VNC Url",webDriverManager.getDockerNoVncUrl().toString());
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND, Keys.SUBTRACT));
findElement(By.id("txtUsername")).sendKeys("Admin");
findElement(By.id("txtPassword")).sendKeys("admin123");
findElement(By.id("btnLogin")).click();
findElement(By.id("menu_admin_viewAdminModule")).click();
findElement(By.id("tableWrapper"));
driver.quit();
webDriverManager.quit();
}
public static WebElement findElement(By by) {
WebElement we = new WebDriverWait(driver, 30).until(d -> d.findElement(by));
return we;
}
}
I tried the following:
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND,Keys.SUBTRACT));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND,Keys.SHIFT,Keys.SUBTRACT));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.COMMAND,"-"));
new Actions(driver).sendKeys(driver.findElement(By.tagName("body"),Keys.chord(Keys.COMMAND,Keys.SUBTRACT)).build().perform();
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keydown',{'key':'Meta'}))",we);
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keydown',{'key':'+'}))",we);
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keyup',{'key':'Meta'}))",we);
js.executeScript("arguments[0].dispatchEvent(new KeyboardEvent('keyup',{'key':'+'}))",we);
options.addArguments("force-device-scale-factor=0.75");
options.addArguments("high-dpi-support=0.75");
WebDriver driver = new ChromeDriver(options);
Only this one worked but its not optimal as the moment I navigate to next screen, the zoom gets resetted.
js.executeScript("document.body.style.zoom='80%'");
I followed all these links but none worked for me. Any help would be much appreciated.
StackOverflowQuestion
Also Robot class doesn't suit me as I had to run in different OSes.

IllegalStateException when running a Selenium test

I'm getting an IllegalStateException thrown when running the following code:
package newprojectss;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Tours {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver","C:\\selenium-2.25.0\\chromedriver_win32");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://demo.guru99.com/test/newtours/";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page with the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
}
}
What can cause the problem?
illegalstateexception occurs because the chrome driver is not set properly
System.setProperty("webdriver.chrome.driver","C:\\selenium-2.25.0\\chromedriver_win32")
Chrome driver path doesn't point till chromedriver.exe file
Unzip your driver folder and point the location till chromedriver.exe
Eg: C://Users//username//Desktop//chromedriver.exe

How to use different webdrivers based on environment

I use selenium-jupiter. I am getting a webdriver from method arguments like this:
#Test
public void testWithChrome(ChromeDriver chromeDriver) {
chromeDriver.get("someUrlHere");
}
Now I want to run tests on grid so I need to use webdriver based on environment. For example when developing tests on my PC I want to use (local) ChromeDriver, but when running tests on grid with Jenkins, I want to use RemoteDriver.
So I need something like this: (That gives me local Chrome when env = 0 or gives me remote Chrome when env = 1 but it's not working)
int env = 0;
#Test
public void testWithChrome(
(env == 0 ? ChromeDriver driver : RemoteDriver driver)) {
driver.get("someUrlHere");
}
In short: When configuring your Selenium extension programmatically you can force usage of a Selenium Grid by configuring its URL as follows (using JUnit 5 annotations):
abstract class UiTest {
#RegisterExtension
static SeleniumExtension seleniumExtension = new SeleniumExtension();
#BeforeAll
static void setUpOnce() {
boolean isRunningInCiEnvironment = ...
if( isRunningInCiEnvironment ) {
// this will force Selenium Jupiter to use a RemoteWebDriver
seleniumExtension.getConfig().setSeleniumServerUrl("http://...");
}
// without above condition, a FirefoxDriver will be used locally
seleniumExtension.addBrowsers(BrowserBuilder.firefox().build(););
}
}
class MyTest extends UiTest {
// Use WebDriver interface in test method: concrete browser detected
// at execution time (via #BeforeAll, inherited from parent class)
#Test
void my_test_Case(WebDriver webDriver) {
webDriver.get(...)
Assert.(...)
}
}
The problem in length is decribed here.
I think what would be better here is to have a method that is executed before any test (annotated with #BeforeAll) that determines what environment the script is being run in. It probably reads from some config file local vs grid. Once that is determined, assign the driver variable either an instance of ChromeDriver or RemoteDriver. From then on, your tests will pass around the driver instance which will be of type WebDriver because both ChromeDriver and RemoteDriver inherit from it.
WebDriver driver;
#BeforeAll
public void setup()
{
// read from config file, etc. to determine if local or grid
if (local)
{
driver = new ChromeDriver();
}
else
{
driver = new RemoteDriver();
}
}
#Test
public void test()
{
driver.get("someUrlHere");
}
You can do that with WebDriverManager that comes with this extension.
#BeforeEach
public void setUp()
{
switch(browser)
{
case "chrome" ->
{
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
case "firefox" ->
{
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
case "edge" ->
{
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
}
}
driver.manage().window().maximize();
}

Strange behaviour in code exection in java

I face strange execution behaviors in login test methods. I run this code Under selenium Grid. and Grid is configured as a standalone server. So, first I start the selenium grid(Hub\Node) using the batch file to execute by tests.
Following is my class and specs.
code:
1. pojDataSource.java:
public class pojDataSource {
private static WebElement element = null;
private static List<WebElement> elements = null;
public static WebElement txt_UserName(WebDriver driver){
driver.findElement(By.id("txtUserName")).clear();
element = driver.findElement(By.id("txtUserName"));
return element;
}
public static WebElement txt_Password(WebDriver driver){
driver.findElement(By.id("txtPassword")).clear();
element = driver.findElement(By.id("txtPassword"));
return element;
}
}
clsConstant.java:
public class clsConstant {
public static final String URL = "http://localhost:1234/";
public static final String Username = "username";
public static final String Password = "password";
}
ModuleTest.java:
public class ModuleTest {
public RemoteWebDriver mDriver = null;
public DesiredCapabilities mCapability = new DesiredCapabilities() ;
public WebElement mWebElement = null;
public String mBaseURL = clsConstant.URL;
public static clsExcelSampleData mAddConnectorXls;
#Test
public void beforeMethod() throws Exception {
WebDriverWait wdw =null;
mCapability.setCapability("platform", org.openqa.selenium.Platform.WINDOWS);
mCapability = DesiredCapabilities.firefox();
mCapability.setVersion("45.0.2");
mDriver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub/"), mCapability);
mDriver.get(mBaseURL);
mDriver.manage().window().maximize();
pojDataSource.txt_UserName(mDriver).sendKeys(clsConstant.Username ) ;
pojDataSource.txt_Password(mDriver).sendKeys(clsConstant.Password ) ;
pojDataSource.btn_LogIn(mDriver).click();
}
When I execute the code in DEBUG mode in eclipese IDE it shows me the strange behaviors. First it start browser and open the mBaseURL successful with login screen. After loading page it shows default userName\password in browser.
Now when debug point comes to pojDataSource.txt_UserName(mDriver).sendKeys(clsConstant.Username ); line. By pressing F5 my debug point goes to pojDataSource.txt_Password(); line and it fetch wrong password and script execution fails. I worry about how this will be happens if my debug point is at username but still it goes to fetch value of password?
Tried solutions:
1. As I use Firefox browser to run test. I clear my password from browser catch.
Recheck the WebElements IDs and make sure they are reachable by WebDriver while debugging. Also try to avoid using 'static' to WebElements. Take a look on Page Objects Pattern.

WebDriver: How to Convert MarionetteDriver to ThreadLocal<WebDriver> for usage in Parallel Test in TestNG

is there any ways to achieve type casting of local instance of WebDriver through ThreadLocal<WebDriver> to MarionetteDriver??? My code goes like this
public class Base_Class
{
protected ThreadLocal<WebDriver> Driver = null;
#BeforeMethod
#Parameters("BrowserName")
public void setUp(#Optional("Firefox") String BrowserName) throws MalformedURLException
{
Driver = new ThreadLocal<WebDriver>();
if(BrowserName.equalsIgnoreCase("FireFox"))
{
System.setProperty("webdriver.gecko.driver", "..//BrowserDrivers//wires");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities);
}
else if(BrowserName.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "..//BrowserDrivers//chromedriver");
Driver = new <ThreadLocal<WebDriver>>ChromeDriver();
}
}
public WebDriver getDriver()
{
return Driver.get();
}
#AfterMethod
public void closeBrowser()
{
getDriver().quit();
}
}
And all the test case are defined in separate classes which extends this above Base_Class.
Getting Error # Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities); and Driver = new <ThreadLocal<WebDriver>>ChromeDriver(); lines as Type mismatch: cannot convert from MarionetteDriver to ThreadLocal<WebDriver>
I am using Chrome Version 52.0.2743.116 (64-bit) and FireFox Version 48.0 version browsers on Ubuntu 14.04 Os and Selenium version selenium-server-standalone-2.53.0
Wanted to achieve parallel test execution through testng.xml file..
any help would be highly appreciated..
You need to make the below changes in your code to make it compile.
Create MarionetteDriver object with your DesiredCapabilities and
Set this Driver object inside ThreadLocal object using its set method.
Like below :
if(BrowserName.equalsIgnoreCase("FireFox")) {
System.setProperty("webdriver.gecko.driver", "..//BrowserDrivers//wires");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
// Commented out below line from your code
//Driver = new <ThreadLocal<WebDriver>> MarionetteDriver(capabilities);
Driver.set(new MarionetteDriver(capabilities));
}
Try this and let me know

Resources