running selenium webdriver test cases against multiple browsers - java

I am new to selenium testing. I want to run selenium test cases on multiple browsers against internet explorer, Firefox, opera and chrome. What approach i have to follow. Can you people please suggest me which is the best process.
Does selenium web driver supports multiple browsers or not???
We had written login script. It runs successful for Firefox, chrome and internet explorer individually. But i want to run it for those multiple browsers sequentially.

web driver supports multiple browsers of course, there is also support for mobile
ChromeDriver
IEDiver
FirefoxDriver
OperaDriver
AndroidDriver
Here is an exemple to run the same tests in multiple browsers.
package ma.glasnost.test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
.........
DesiredCapabilities[] browserList = {DesiredCapabilities.chrome(),DesiredCapabilities.firefox(),DesiredCapabilities.internetExplorer(), DesiredCapabilities.opera()};
for (DesiredCapabilities browser : browserList)
{
try {
System.out.println("Testing in Browser: "+browser.getBrowserName());
driver = new RemoteWebDriver(new URL("http://127.0.0.1:8080/..."), browser);
Hope that helps.

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Sample {
private WebDriver _driver;
#Test
public void IEconfiguration() throws Exception {
System.setProperty("webdriver.ie.driver",
"D:/Softwares/Selenium softwares/drivers/IEDriverServer.exe");
_driver = new InternetExplorerDriver();
login();
}
#Test
public void FFconfiguration() throws Exception {
_driver = new FirefoxDriver();
login();
}
#Test
public void CRconfiguration() throws Exception {
System.setProperty("webdriver.chrome.driver",
"D:/Softwares/Selenium softwares/drivers/chromedriver.exe");
_driver = new ChromeDriver();
//_driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
login();
}
public void login() throws Exception {
_driver.get("http://www.google.com");
}
}
Before that we have to install the chrome and internet explorer drivers .exe files and run those.

You could use the WebDriver Extensions framework's JUnitRunner
Here is an example test googling for "Hello World"
#RunWith(WebDriverRunner.class)
#Firefox
#Chrome
#InternetExplorer
public class WebDriverExtensionsExampleTest {
// Model
#FindBy(name = "q")
WebElement queryInput;
#FindBy(name = "btnG")
WebElement searchButton;
#FindBy(id = "search")
WebElement searchResult;
#Test
public void searchGoogleForHelloWorldTest() {
open("http://www.google.com");
assertCurrentUrlContains("google");
type("Hello World", queryInput);
click(searchButton);
waitFor(3, SECONDS);
assertTextContains("Hello World", searchResult);
}
}
just make sure to add the WebDriver Extensions framework amongst your maven pom.xml dependencies
<dependency>
<groupId>com.github.webdriverextensions</groupId>
<artifactId>webdriverextensions</artifactId>
<version>1.2.1</version>
</dependency>
The drivers can be downloaded using the provided maven plugin. Simply add
<plugin>
<groupId>com.github.webdriverextensions</groupId>
<artifactId>webdriverextensions-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<goals>
<goal>install-drivers</goal>
</goals>
</execution>
</executions>
<configuration>
<drivers>
<driver>
<name>internetexplorerdriver</name>
<version>2.44</version>
</driver>
<driver>
<name>chromedriver</name>
<version>2.12</version>
</driver>
</drivers>
</configuration>
</plugin>
to your pom.xml. Or if you prefer downloading them manually just annotate the test class with the
#DriverPaths(chrome="path/to/chromedriver", internetExplorer ="path/to/internetexplorerdriver")
annotation pointing at the drivers.
Note that the above example uses static methods from the WebDriver Extensions Bot class to make the test more readable. However you are not tied to using them. The above test rewritten in pure Selenium WebDriver would look like this
#Test
public void searchGoogleForHelloWorldTest() throws InterruptedException {
WebDriver driver = WebDriverExtensionsContext.getDriver();
driver.get("http://www.google.com");
assert driver.getCurrentUrl().contains("google");
queryInput.sendKeys("Hello World");
searchButton.click();
SECONDS.sleep(3);
assert searchResult.getText().contains("Hello World");
}

Related

How to run a Java Selenuim test on a certain Chrome profielr [duplicate]

This question already exists:
How to run a Java Selenium test on an incognito Google Chrome profile
Closed 2 years ago.
I hope that you're fine. I'm new at Java Selenium automation, what I want to do is I want to run a test on a certain website but not in a clean session of ChromeDriver I would like to use a certain profile of mine I name it "test" its shortcut in the Desktop is "test.lnk", here what I did:
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
//import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Test;
public class AutomationTest {
WebDriver driver;
#BeforeTest
public void setUp() {
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/test");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
// driver = new ChromeDriver();
driver.get("https://www.rapidtables.com/tools/click-counter.html");
driver.manage().window().maximize();
}
#Test
public void testRegister() throws InterruptedException {
do {
Thread.sleep(3000);
driver.findElement(By.id("addbtn")).click();
} while(true);
}
#AfterTest
public void tearDown() {
// driver.close();
}
}
```
chrom_options.add_argument("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")
chrom_options.add_argument("profile-directory=test)
specify only the user data folder in userdata dir argument
Pass profile directory separately

Relative Xpath Not Work In Maven TestNg But Absolute Path Works

Before installing/using Maven plugins, all absolute & relative paths worked in testNG. But After using maven, Relative Xpath not worked.
Even I tried by my own Relative Xpath but not worked.
I used Eclipse IDE for JAVA, Selenium 3.141.59 . Here I attached my 2 codes of src/main/java and src/test/java respectively.
phpTravelsLoginPage.java
package sqa;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class phpTravelsLoginPage {
WebDriver driver;
/* Here This Below XPath works */
By MyAccount = By.xpath("/html/body/nav/div/div[2]/ul[2]/ul/li[1]/a");
By loginBtn = By.xpath("/html/body/nav/div/div[2]/ul[2]/ul/li[1]/ul/li[1]/a");
/* But Here This Below XPath works */
// By MyAccount = By.xpath("//*[#id=\"li_myaccount\"]/a");
// By loginBtn = By.xpath("//*[#id=\"li_myaccount\"]/ul/li[]/a");
public phpTravelsLoginPage(WebDriver driver)
{
this.driver = driver;
}
// CLick on MyAccount Dropdown
public void clickOnMyAccount()
{
driver.findElement(MyAccount).click();
}
// CLick on Login Dropdown button
public void clickOnLoginButton()
{
driver.findElement(loginBtn).click();
}
/* This loginToPhptravels method will be exposed to the Test Case */
public void loginToPhptravels() throws InterruptedException
{
// Click on the MyAccount Dropdown
this.clickOnMyAccount();
// Click on the Login dropdown menu
this.clickOnLoginButton();
}
}
loginTest.java
package sqa;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import sqa.phpTravelsLoginPage;
public class loginTest {
WebDriver driver;
phpTravelsLoginPage php;
#Test
public void f() throws InterruptedException {
driver.get("https://phptravels.net");
php = new phpTravelsLoginPage(driver);
php.loginToPhptravels();
}
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver = new FirefoxDriver();
}
#AfterTest
public void afterTest() {
}
}
POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sqa</groupId>
<artifactId>sqa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
Note : TestNG installed by "Install New Software" , not through maven.
Testcases having absolute xpaths are not very consistent in execution, it is recommended that Do Not Use absolute Xpath, and it is nothing to do with installing maven. After searching little bit found below working and unique matches of xpaths hope those will work for you!!!
By myaccount = By.xpath(".//a[#href='https://www.phptravels.net/']/ancestor::div[2]//a[text()=' My Account ']");
By Login = By.xpath(".//a[text()=' My Account ' and #aria-expanded]/following-sibling::ul//a[text()=' Login']");

Unable to Launch chrome (version 67) window 7 64 bit

Not able to launch my chrome browser(version 67) , please refer below mentioned code and screenshot
package FirstPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\XY56082\\Desktop\\sel\\hromedriver\\chromedriver.exe");
//FirefoxDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
//driver.get("http://www.google.com");
// Open Google
driver.get("http://www.google.com");
// Close browser
driver.close();
}
}
May be you should change your driver to:
chromedriver-2.35.exe
with chrome 67 it is the perfect driver.
I am using selenium 3.8.1 in maven.
Hope that helps you.

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system

Following previous related issues posted and given resolution, I tired everything but still getting same error for FireFox, Chrome & Internet Explorer.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Search {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
System.getProperty("webdriver.gecko.driver",
"C:\\Users\\nitin\\Downloads\\geckodriver-v0.18.0-
win64\\geckodriver.exe");
driver.get("http://www.wikipedia.org");
WebElement link;
link = driver.findElement(By.linkText("English"));
link.click();
Thread.sleep(5000);
WebElement searchbox;
searchbox = driver.findElement(By.id("searchInput"));
searchbox.sendKeys("Software");
searchbox.submit();
Thread.sleep(5000);
driver.quit();
Shouldn't that be System.setProperty() instead of .getProperty()?
System.setProperty("webdriver.gecko.driver, "C:\\Users\\...\\geckodriver.exe");
Use that gecko driver system property before driver intialization
So first line gecko property and next line driver=new so and so..
Use .setProperty and declare it after providing the path to webdriver
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\nitin\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

I couldnt find the fault in my code for login page test with selenium java

package automation;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MainPage {
private final WebDriver driver;
public MainPage(WebDriver driver) {
this.driver = driver;
}
public MainPage loginAs(String username, String password) {
driver.get("URL");
driver.findElement(By.name("email")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.className("login")).click();
return new MainPage(driver);
}
public static void main(String[] args) {
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
MainPage login = new MainPage(new ChromeDriver());
login.loginAs("qa#gmail.com", "123456");
}
}
I tried to test login function for the web site and wrote above code for it. However,
I couldnt find the error on it. Can someone help me to figure it out?
I see a few things wrong with your code:
System.setProperty("webdriver.genko.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
Should be (make sure the chromedriver is the correct file, the windows version usually has an .exe extension.)
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Guest01\\Desktop\\chromedriver");
You also should navigate the url of the page you want to test using
WebDriver driver = new ChromeDriver();
driver.get("http://[enter url here]")
Please write the "gecko driver" instead of "genko driver".

Categories