Automating switching browsers when running Selenium-Java tests - java

I am currently working on a project using Java, Selenium and Testng. My overall goal is to test the functionality of a webpage over different web browsers. I have my Selenium code working and am able to run the test on Chrome and Firefox. However, I have to manually change the code to switch the browser. I do this by commenting out driver = new ChromeDriver();
I would like to edit my code so the test runs in Firefox and when that test is complete start the test in Chrome. Can someone please guide me in the right direction?
Here is a sample of what my code looks like:
WebDriver driver = null;
Selenium selenium = null;
#BeforeSuite
public void setup() throws Exception {
/// Chrome Driver ///
System.setProperty("webdriver.chrome.driver", "mac/chromedriver.exe");
//driver = new ChromeDriver();
/// Firefox Driver ///
driver = new FirefoxDriver();
}
#Test
public void testGoogle() throws Exception {
selenium = new WebDriverBackedSelenium(driver,"URL");

There could be quite a few ways of achieving this.
In the setup you can read a property and based on that you can instantiate the right driver.
String driverType = System.getProperty("driverType");
if ("firefox".equals(driverType))
driver = new FirefoxDriver().....
You can run the test twice, once with firefox property and then with chrome property.
Other option is to keep all the test in one class. Then extend this class by two classes, one for firefox setup and another for chrome setup. Then you can run both the subclass tests in a suite. They would run one after the another.

Related

How to use chrome profile when using browserInDocker with WebDriverManager?

I'm using webdrivermanager with browserInDocker() for my test automation framework. So far it works great. But now I have trouble trying to use a chrome profile.
Usually, when I use selenium webdriver, I simply can set a ChromeOption and it works.
How can I use it with browserInDocker()?
I tried the following code:
public void setupBrowserInDockerWithProfile() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=/home/seluser/.config/google-chrome/Default");
driver = WebDriverManager
.chromedriver()
.capabilities(options)
.browserInDocker()
.dockerVolumes("/var/lib/docker/volumes/chromeprofile/_data:/home/seluser/.config/google-chrome/Default")
.create();
driver.manage().window().maximize();
driver.get(APP_URL);
}
I tried it with and without a docker volume, but I'm not even sure if I use it right. I'm searching for days now, but could't find a solution yet.
The problem is the argument --user-data-dir=/home/seluser/.config/google-chrome/Default. As explained in the doc, WebDriverManager uses the Aerokube's Docker images. That path is available in the docker-images, but not in Aerokube's. You can drop that argument or change it to the following:
options.addArguments("--user-data-dir=/home/selenium/.config/google-chrome/Default");

WebDriver isn't launching the ChromeDriver

I am developing a test case using selenium to verify that a user has reached the properties list on a travel site. When i go to run the application on eclipse, the driver is failing.
I have tried using the "WebElement" method as another option but, that is giving me the same issue.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Extracted Files\\Chrome driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.booking.com/");
driver.manage().window().maximize();
driver.findElement(By.className("input-location.location-field.px-2")).sendKeys("Krakow, Poland");
Thread.sleep(2000);
driver.findElement(By.className("btn btn-primary btn-block mt-4 submit-search-form")).click();
Thread.sleep(2000);
driver.close();
}
Expected result is to bring up all the listing for the searched area on the properties page. Actual result is the application is simply failing to execute and is highlighting the "WebDriver driver = new ChromeDriver" line.
Any help or assistance/documentation would be greatly appreciated.

What is the best way to deal with multiple browsers in selenium?

Let me explain the problem statement :
I want to design Page Object Model for a page using selenium. And the requirement is, scripts executing on multiple browsers will use this class. How should I deal with element locators in my Page class ? What I can think of is
Get the driver object, and using if else, pick the browser specific XPath for locating the element . Like if the driver is chrome then locateChromeElement.
Create different page classes for different browsers.
Create base page class and extend it based on browsers.
What is the best way? What is used in industry?
It depends entirely on your AUT (Application under test). If you have different locators for the same webelement on a page (e.g. in case of multi-lingual sites), then use properties file for storing the webelements and name it as per your page (e.g. for HomePage class you can have different files HomePage.properties (English), HomePage_it.properties (Italian) etc.)
Usually, if you go for CSS for location webelement, you will find it same for almost every browser.
The xpaths will be the same regardless of which browser you use.
to make a script work in multiple browsers, you can create multiple TestNg suites for different browsers, and have the same script for all the suites.
All you need to change in the suites are the Browser classes.
Consider the following script
You can run this entire code in one go. All the test suites will be executed one after the other
class MultipleBrowser{
//for Firefox
#Test
public void FirefoxBrowser()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("Automating in firefox Browser");
}
//for ChromeBrowser
#Test
public void ChromeBrowser()
{
WebDriver driver = new ChromeDriver(); //only the class is changed from firefoxDriver to ChromeDriver
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("Automating in Chrome Browser");
}
//for InternetExplorer
#Test
public void IEBrowser()
{
WebDriver driver = new InternetExplorerDriver(); //only the class is changed from ChromeDriver to IEDriver
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("Automating in IE Browser");
}
}

Unable to locate the elements of SmartGWT application using Selenium in IE browser

I'm testing a GWT + SMARTGWT application like Paint and I'm trying to locate the elements of this web application using Selenium Webdriver. The method which I have used to locate the elements is by the relative XPath of those elements but the problem which I am currently facing is that this method is working correctly on the browsers like Chrome, Firefox, and Edge but not on the IE browser. The version of IE on my PC is 11.1593.14393.0. In the IE browser, this relative XPath method is giving a TimeOutException. I have given the explicit wait:
wait.until(ExpectedConditions.elementToBeClickable(webelement));
The IE browser is not able to find the element. I am also getting the following exception sometimes for other elements:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Unable to locate an element with the xpath expression //img[contains(#src,'Insert XXX'] because of the following error:
Error: Bad token: ]
Among the troubleshooting solutions to this issue, I tried enabling/disabling the protected mode in IE for all the levels but this method didn't work. Along with that, I also tried checking the box next to the option - "Allow active content to run files on My Computer" but this method also failed to work.
What should I do to fix my issue?
This is my code. Here firstly, I will click on the Insert button located on the top bar of the application and after clicking on the Insert button, a window will launch on which I will click on the Close button.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\SELENIUM\\Drivers\\iedriverserver.exe");
WebDriver driver = new InternetExplorerDriver();
Thread.sleep(3000);
driver.get(baseURL);
WebDriverWait wait = new WebDriverWait(driver, 10);
final String InsertPath = "//img[contains(#src,'Insert XXXX')]";
final String closePath="//img[contains(#src,'close')]";
WebElement Insert = driver.findElement(By.xpath(InsertPath));
wait.until(ExpectedConditions.elementToBeClickable(Insert));
Thread.sleep(2000);
Insert.click();
WebElement close = driver.findElement(By.xpath(closePath));
wait.until(ExpectedConditions.elementToBeClickable(close));
Thread.sleep(3000);
close.click();
}
}
Edit: I also used finding the element using Javascript executor in my code as follows:
WebElement Insert = driver.findElement(By.xpath(InsertPath));
Thread.sleep(2000);
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", Insert);
Sadly, this method also failed to work in the IE browser.
So, I was able to locate the elements by using the latest driver of the Internet Explorer and giving the following desired capabilities in my code to the IE browser.
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability("requireWindowFocus", true);
ieCapabilities.setCapability("unexpectedAlertBehaviour", "accept");
ieCapabilities.setCapability("ignoreProtectedModeSettings", true);
ieCapabilities.setCapability("disable-popup-blocking", true);
ieCapabilities.setCapability("enablePersistentHover", true);*/
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(ieCapabilities);

Selenium WebDriver Automation for Vaadin components

I am using Selenium Java WebDriver to automate a Vaadin based web application. When I navigate to a page and click a button it opens up another small window which does not look like a popup to me. It seems the subwindow is dynamically added to the main html.
Does anyone have any idea on how to identify Vaadin elements in Selenium?
try with
driver.findElement(By.className("v-window")) for the entire window or
driver.findElement(By.className("v-window-contents")) for the window content.
When you creating test be sure to run this test after the browser is open. Sometimes test starts, but browser is not open. I put Thread.sleep(1000); in #Before class, to be sure chrome is open.
#Before
public void openBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "chromedriver");
driver = new ChromeDriver();
driver.get("http://localhost:8000");
Thread.sleep(1000);
}
and then
#Test
public void testLoginFormExists() {
WebElement login = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
...
}
or even better, check this answer: https://stackoverflow.com/a/9430377/1168786

Categories

Resources