I'm trying to open a website in three browser using selenium grid. when i'm running my script,firefox and ie working fine.But chrome browser is opening with "data:," not with a URL.
Selenium Version 2.47
Chromedriver.exe 2.20
Chrome 46.0
Can any one tell me why?
#Parameters("browser")
#BeforeTest
public void launchapp(String browser) throws MalformedURLException
{
String URL = "http://www.tutorialspoint.com/selenium/selenium_grids.htm";
if (browser.equalsIgnoreCase("firefox"))
{
System.out.println(" Executing on FireFox");
String Node = "http://10.101.7.220:5555/wd/hub";
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
driver = new RemoteWebDriver(new URL(Node), cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("ie"))
{
System.out.println(" Executing on IE");
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities
.internetExplorer();
capabilities
.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
capabilities.setCapability("ignoreZoomSetting", true);
capabilities.setCapability("nativeEvents", false);
//driver = new RemoteWebDriver(new URL(Node), capabilities);
driver= new InternetExplorerDriver(capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch website
driver.navigate().to(URL);
driver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("chrome"))
{
System.out.println("Running Chrome");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\mob150003576\\Downloads\\chromedriver.exe");
driver = new ChromeDriver();
}
else
{
throw new IllegalArgumentException("The Browser Type is Undefined");
}
}
#Test
public void sample()
{
driver.findElement(By.xpath("//a[text()=' Home']")).click();
}`
One thing I notice in your code is that you do not use RemoteWebDriver for running your IE or Chrome tests. The code can be simplified to read:
String URL = "http://www.tutorialspoint.com/selenium/selenium_grids.htm";
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
DesiredCapabilities caps = null;
switch(browser){
case "chrome" : caps = DesiredCapabilities.chrome();
break;
case "firefox" : caps = DesiredCapabilities.firefox();
break;
case "ie" : caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
break;
}
driver = new RemoteWebDriver(new URL("grid string"), caps);
driver.navigate().to(URL);
driver.manage().window().maximize();
Please update the paths to browser driver as per your PC and add the specific capabilities you need.
Related
I have a .properties file that gives input data for instantiating the WebDriver inside the ServiceHooks class.
How can I change this logic to use a cucumber .feature file to drive the parameters that I provide to the WebDriver and implement such a scenario?
Feature: Login page navigation
Scenario Outline: User navigates to url
Given I open the browser <browser> on operatingSystem <platform>
When I navigate to url <url>
Then I see the login page
Examples:
|browser|platform| url |
|"firefox"|"windows"|"https://loginpage.com"|
|"chrome"| "windows" |"https://loginpage.com"|
I have some code that evaluates if the driver is remote or not and instantiates the driver (thus opens the browser already) before the test scenario gets executed.
public class ServiceHooks {
public static WebDriver driver;
public void getDriverInstance() {
//if isRemoteDriver evaluests to true or false base on a .properties file
if (isRemoteDriver.equals("yes")){
driver = getRemoteDriverInstance(platform, browser, url);
} else {
driver = getLocalDriverInstance(platform, browser, url);
}
//platform, browser, url are served from the .properties file as well
public WebDriver getLocalDriverInstance(String platform, String browser, String url)
throws MalformedURLException {
WebDriver localDriver = null;
DesiredCapabilities capabilities = new DesiredCapabilities();
//ChromeOptions options = new ChromeOptions();
// Platforms
if (platform.equalsIgnoreCase("Windows")) {
capabilities.setPlatform(Platform.WINDOWS);
}
if (platform.equalsIgnoreCase("MAC")) {
capabilities.setPlatform(Platform.MAC);
}
// Browsers
if (browser.equalsIgnoreCase("chrome")) {
capabilities = DesiredCapabilities.chrome();
localDriver = new ChromeDriver();
}
if (browser.equalsIgnoreCase("firefox")) {
capabilities = DesiredCapabilities.firefox();
localDriver = new FirefoxDriver();
}
if (browser.equalsIgnoreCase("ie")) {
capabilities = DesiredCapabilities.internetExplorer();
localDriver = new InternetExplorerDriver();
}
localDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
localDriver.manage().window().maximize();
localDriver.get(url);
return localDriver;
}
public static WebDriver getRemoteDriverInstance(String platform, String browser, String url)
throws MalformedURLException {
String nodeURL = "http://localhost:4444/wd/hub";
WebDriver remoteDriver = null;
DesiredCapabilities capabilities = new DesiredCapabilities();
// Platforms
if (platform.equalsIgnoreCase("Windows")) {
capabilities.setPlatform(Platform.WINDOWS);
}
if (platform.equalsIgnoreCase("MAC")) {
capabilities.setPlatform(Platform.MAC);
}
// Browsers
if (browser.equalsIgnoreCase("chrome")) {
capabilities = DesiredCapabilities.chrome();
}
if (browser.equalsIgnoreCase("firefox")) {
capabilities = DesiredCapabilities.firefox();
}
if (browser.equalsIgnoreCase("ie")) {
capabilities = DesiredCapabilities.internetExplorer();
}
// Open the Application
remoteDriver = new RemoteWebDriver(new URL(nodeURL), capabilities);
remoteDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
remoteDriver.manage().window().maximize();
remoteDriver.get(url);
return remoteDriver;
}
}
I want to be able to run the mentioned cucumber test scenario locally using WebDriver an well as remotely with Selenium Grid.
I have written a script for Cross Browser Testing in Selenium Webdriver using TestNG Parameters.But In this script,driver.manage().window()
.maximize(); is not working from chrome.How Could I maximize all browsers' window?
public class WithTestNG {
WebDriver driver;
#Parameters("browser")
#BeforeClass
{
if(browser.equalsIgnoreCase("firefox"))
System.setProperty("webdriver.gecko.driver","/Users/Preet/Desktop
/Path/geckodriver" );
driver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("ie"))
{
System.setProperty("webdriver.ie.driver","C:\\Users\\CP\\Downloads\\IED
riverServer_x64_3.4.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else if (browser.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver","/Users/Preet12/Desktop/Pa
th/chromedriver" );
driver = new ChromeDriver();
}
driver.manage().window().maximize();
}
#Test(priority=0)
public void OpenStore()
{
String URL = "www.facebook.com";
driver.get(URL);
String Actual_URL = driver.getCurrentUrl();
String Expected_URL = "https://www.facebook.com/";
Assert.assertEquals(Actual_URL, Expected_URL, "URL doesn't match");
System.out.println("URL verified");
}
Replace:
driver.manage().window().maximize();
with:
driver.manage().window().fullscreen();
When I run the following code, below error is showing : org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
The chrome browser is not getting launched.
//baseClass.java:
public class BaseClass {
//ThreadLocal will keep local copy of driver
public static ThreadLocal<RemoteWebDriver> dr = new ThreadLocal<RemoteWebDriver>();
#BeforeTest
//Parameter will get browser from testng.xml on which browser test to run
#Parameters("myBrowser")
public void beforeClass(String myBrowser) throws MalformedURLException{
try {
RemoteWebDriver driver = null;
if(myBrowser.equals("chrome")){
DesiredCapabilities capability = new DesiredCapabilities().chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
else if(myBrowser.equals("firefox")){
DesiredCapabilities capability = new DesiredCapabilities().firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
//setting webdriver
setWebDriver(driver);
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}catch (Exception ex){
System.out.println(ex.toString());
}
}
public WebDriver getDriver() {
return dr.get();
}
public void setWebDriver(RemoteWebDriver driver) {
dr.set(driver);
}
#AfterClass
public void afterClass(){
getDriver().quit();
dr.set(null);
}
}
You have to set the system property for chrome/gecko driver before initializing the RemoteWebDriver. Something like,
if(myBrowser.equals("chrome")){
DesiredCapabilities capability = new DesiredCapabilities().chrome();
capability.setBrowserName("chrome");
capability.setPlatform(Platform.WINDOWS);
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
else if(myBrowser.equals("firefox")){
DesiredCapabilities capability = new DesiredCapabilities().firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WINDOWS);
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe");
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
}
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","D:/chrome driver/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://app.mycable.in/#/login");
driver.findElement(By.xpath("//input[#name='username']")).sendKeys("mycable");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("mycable1");
driver.findElement(By.xpath("//input[#name='submit']")).click();
Thread.sleep(1000);
Alert alert =driver.switchTo().alert();
alert.accept();
driver.findElement(By.xpath(".//a[contains(href='#/customer/')][text='restricted.customer']")).click();
driver.findElement(By.xpath("//input[#value='input']")).sendKeys("abcd");
}
Just use Incognito mode in Chrome to avoid any alerts/notifications and Incognito is also secure in some ways.
Just run the below code.
public static void main(String[] args) throws InterruptedException
{
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver=new ChromeDriver(capabilities);
driver.get("http://app.mycable.in/#/login");
driver.get("http://app.mycable.in/#/login");
driver.findElement(By.xpath("//input[#name='username']")).sendKeys("mycable");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("mycable1");
driver.findElement(By.xpath("//input[#name='submit']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//a[contains(href='#/customer/')][text='restricted.customer']")).click();
driver.findElement(By.xpath("//input[#value='input']")).sendKeys("abcd");
}
To avoid that Remember username and Password popup these piece of code may help full to you.
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.chrome();
chromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
There is no use of saving password everytime because at every time of execution new instance of driver will be invoked with no cookies or cache in it. Let me know if there is any issue
Here is the solution for your question:
As per standard practices it is recommended not to use Thread.sleep(1000) instead use Implicitwait as below:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Save username/password is not an Alert. We will disable it using Options Class of Chrome as below:
System.setProperty("webdriver.chrome.driver", "C:\\SeleniumUtilities\\BrowserDrivers\\chromedriver.exe");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("--disable-extensions");
options.addArguments("--disable-notifications");
options.addArguments("--enable-automation");
options.addArguments("--disable-save-password-bubble");
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://app.mycable.in/#/login");
driver.findElement(By.xpath("//input[#name='username']")).sendKeys("mycable");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("mycable1");
driver.findElement(By.xpath("//input[#name='submit']")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Using the following code with options for Chrome driver will help you to get rid of all issues.
Let me know if this helps you.
private WebDriver driver;
#BeforeMethod
public void setUp() throws Exception {
// set up appium
BasicConfigurator.configure();
File appDir = new File("This PC\\GT-I9100\\Phone\\360");
File app = new File(appDir, "app-release.apk"); //my case “demo1.apk”
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOW");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "app-release.apk"); //my case com.gorillalogic.monkeytalk.demo1
capabilities.setCapability("app-activity", "Login"); //my case RootActivity
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void loginTest() throws Throwable
{
System.out.println("Hello");
System.out.println(driver);
setUp();
}
#Test
public void formTest() throws InterruptedException
{
System.out.println("Hello");
System.out.println(driver);
/*Getting driver value as null in selenium web driver for appium mobile automation testing
Getting driver value as null.
Used device name then also i am getting null value
I have connected my real device.*/
Add a try/catch block when instantiating the AndroidDriver() ...
Maybe there is something wrong there. Try this code
new DesiredCapabilities();
DesiredCapabilities capabilities = DesiredCapabilities.android();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"0123456789ABCDEF");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4");
try
{
linker = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver(linker, capabilities);
driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
}
catch (MalformedURLException e)
{
System.out.println("URL init error");
}
Cheers