Failed to maximize Safari browser on MAC with Selenium Webdriver

We are using Selenium with Java to automate our web application. We have working code to run automated test scripts on Windows operating system for three browsers (IE, Chrome, Firefox - latest version).

We have a requirement to run automated test scripts on the MAC operating system, the Safari browser.

Environment Details:

MAC OS version : macOS Sierra version 10.12.5

Safari browser version : 10.1.1 (12603.2.4)

Selenium standalone server version : 3.4.0

Java version : 1.8.0_112

Connected MAC machine with VNC viewer (Sys admin team provided MAC for testing).

When running test scripts on MAC, below code does not maximize Safari browser which works fine for other browsers (IE, Chrome and Firefox) on Windows. Due to this, we cannot find some controls in the application.

driver.manage().window().maximize();

      

We don't get any exceptions, the code does, but does nothing.

Please help to overcome Safari browser with maximum bang for your buck on MAC.

Indeed I thought my problem was solved with this solution, tried the code to maximize the Safari browser but getting an exception.

code:

 SafariOptions options = new SafariOptions();
options.setUseCleanSession(true);
driver = new SafariDriver(options);
JavascriptExecutor jse = (JavascriptExecutor)driver;
String screenWidth = jse.executeScript("return screen.availWidth").toString();
String screenHeight = jse.executeScript("return screen.availHeight").toString();
int intScreenWidth = Integer.parseInt(screenWidth);
int intScreenHeight = Integer.parseInt(screenHeight);
org.openqa.selenium.Dimension d = new org.openqa.selenium.Dimension(intScreenWidth, intScreenHeight);
driver.manage().window().setSize(d);

      

An exception:

07 Aug 2017 3:11:53 pm org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Dialect detected: OSS Exception on stream "main" org.openqa.selenium.NoSuchWindowException: Request to use window could not be honored because the window could not be found. (WARNING: the server did not provide any stack information) Command duration or timeout: 27 milliseconds. Build info: version: '3.4.0', version: '5234b32', time: '2017-03-10 09:04:52 -0800' System info: host: 'Mac.local', ip: 'fe80: 0: 0: 0: 4c6: 11dc: 3f91: 11f8% en0 ', os.name: "Mac OS X", os.arch:' x86_64 ', os.version: '10 .12.6', java.version: " 1.8.0_121 "Driver Information: Features org.openqa.selenium.safari.SafariDriver [{applicationCacheEnabled = true, rotatable = false, databaseEnabled = true, handleAlerts = true, version = 12603.3.8, cleanSession = true, platform = MAC, nativeEvents = true, locationContextEnabled = false, webStorageEnabled = true, browserName = safari, javdascriptE = true, platformName = macOS, cssSelectorsEnabled = true}] Session Id: BA265536-18D3-490E-B6DB-40D8BBF25937 at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at sun.refccessAccess.NativeConstructor.Impler. ) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance (Constructor.java:423) at org.openqa.selenium.remote.ErateHandler.create216) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed (ErrorHandler.java:168) at org.openqa.selenium.remote.RemoteWebDriver.execute (RemoteWebDriver.java:638qa.moteg.opentever $ RemoteWebDriverOptions $ RemoteWindow.setSize (RemoteWebDriver.java:860) at Sample.Safari.main (Safari.java:20)

+3


source to share


2 answers


Safari browser issue was resolved after updating Safari version to 11.0 (12604.1.35)

And working code



driver.manage().window().maximize();

      

+3


source


We are seeing a bug with the new driver libraries. You can use slightly old jars that can handle newer versions of browsers.

You can also use another option to maximize the browser window.

Example: -

Add the parameter below and pass it to the driver: -

    chromeOptions.addArguments("--start-maximized");

      

The complete code will look like this: -

System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);

      

OR



Toolkit toolkit = Toolkit.getDefaultToolkit();
int Width = (int) toolkit.getScreenSize().getWidth();
int Height = (int)toolkit.getScreenSize().getHeight();
//For Dimension class, Import following library "org.openqa.selenium.Dimension"  
driver.manage().window().setSize(new Dimension(Width,Height));
driver.get("https://google.com");

      

OR



((IJavaScriptExecutor)driver).ExecuteScript("window.resizeTo(1024, 768);");

      

Try this on safari: -

JavascriptExecutor jse = (JavascriptExecutor)driver;
String screenWidth = jse.executeScript("return screen.availWidth").toString();
String screenHeight = jse.executeScript("return screen.availHeight").toString();
int intScreenWidth = Integer.parseInt(screenWidth);
int intScreenHeight = Integer.parseInt(screenHeight);
Dimension d = new Dimension(intScreenWidth, intScreenHeight);
driver.manage.window.setSize(d);

      

A detailed example will be given below in the article: -

http://www.abodeqa.com/2015/01/20/maximize-window-using-selenium-webdriver-and-by-using-abstract-window-toolkit/

Hope this helps you :)

0


source







All Articles