Chrome Browser window is not maximize in Selenium-grid node - java

Windows are not maximize issue occurs only in grid node.
we tried the different methods to get it resolved as shown below, but nothing has worked
Dimension dimension = new Dimension(1920,1080)
driver.manage().window().setSize(dimension);
driver.manage().window().maximize()
options.addArguments("--window-size=1920,1080");
options.addArguments("--start-maximized");
NOTE: No issue observed when we run the scripts locally , or if i logged into that node when the scripts is running, but when I close that node, and start the script, then window size would remain small and couldn't find some webElement

Maximize the browser with Java :
driver.manage().window().maximize();
Another way to do with Java:
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenResolution = new Dimension((int)
toolkit.getScreenSize().getWidth(), (int)
toolkit.getScreenSize().getHeight());
driver.manage().window().setSize(screenResolution);

Related

Selenium can't capture the whole element because of monitor size

When using a website and capturing pictures of specific elements on screen,
Sometimes we encounter a problem in which the element is not captured in its fullest form.
After investigating the issue I understand it happens when the Chrome browser opens on laptop screen, which is smaller and that's why the elements are not shown completely.
How can I solve this issue? It also happens on Jenkins sometimes, how is that?
Here is my code snippet:
byte[] imageResult = element.getScreenshotAs(OutputType.BYTES);
BufferedImage imageSnapshot = ImageIO.read(new ByteArrayInputStream(imageResult));
You can increase the Set the Browser resolution to bigger size and pass it in chrome options. that wil increase the browser window size and then scrollinto that element and continue with it.
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1200");
WebDriver driver = new ChromeDriver(options);

how to maximise window size in htmlunit driver?

I am running selenium test using HtmlUnitDriver but at some point test keep failing. Is there any way we can maximize the window size in HtmlUnitDriver?
To maximize the window just use HtmlUnitWindow maximize()
HtmlUnitDriver htmlDriver = new HtmlUnitDriver();
htmlDriver.HtmlUnitWindow().maximize()

Is there any way to minimize chrome window in Selenium?

Something like driver.manage().window().maximize(); but for minimize the window.
Thanks!
Selenium doesn't have minimize() option, atleast not for Java, however you can use setPosition do do it
driver.manage().window().setPosition(new Point(0, 0));
However the better way is to run it as headless browser
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);
This way you can use maximized browser while it's running in the background.
Selenium's java client have no built-in method for minimizing the browser. Ideally, you shouldn't minimize the browser while the Test Execution is In Progress as Selenium would loose the focus over the Browsing Context and an exception will be raised at any point of time which will halt the Test Execution.
You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized
However, to mimic the functionality of minimizing the Browsing Context you can use the following solution:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setSize(new Dimension(0,0));
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
You can set the position of the WebDriver outside of your view. That way, it'll be out of sight while it runs.
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-2000, 0));
OR
Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);
Use below code to completely minimize it.
driver.manage().window().setPosition(new Point(-2000, 0))

Webdriver hangs in new window with “about:blank” in address bar, transitioning to file download dialog

I need some help for the following issue using Webdriver, Java, and Firefox.
In the testing, when clicking on a link,
1) it will often open a new window with a normal web page. OR
2) occasionally, it will open a new window with “about:blank” in address bar; after 20 to 60 seconds the new window will disappear and a file download window will appear.
Because the URL is rewritten for SEO, there is no way to check the URL before or after clicking on the link to determine whether the link connects to a normal web page or a downloadable file.
In both cases,
driver.getWindowHandles().size() == 2,
so I can switch to the new window successfully using the following statements in order to check whether a certain WebElement exists in new window.
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
But if the link connects to a downloadable file, the execution will hang for any of the following methods:
findElement(By.xpath(“//html”));
findElements(By.tagName(“body”));
getCurrentUrl();
getPageSource();
getTitle();
getWindowHandle();
getWindowHandles() always returns 2 while new window with “about:blank” in address bar presents before it is replaced by the file download dialog. It occasionally throws a NoSuchWindowException exception; but most of the time, it just hangs. I tried the Explicit and Implicit Waits to no avail.
Many thanks
Sam
Have you tried setting the following preference? (This will get rid of the download dialog all together)
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.manager.showWhenStarting",false);
FirefoxDriver driver = new FirefoxDriver(profile);

How to resize current browser window in Selenium WebDriver with Java?

I need to resize the browser as 300x400 during executing the Selenium automated tests. How can I resize the browser window in Selenium WebDriver (aka, Selenium 2) with Java?
[Note: It needs to resize the browser window for testing Responsive Web Design (RWD)]
You can get a reference to the current window with driver.manage().window(). And the window has a setSize() method, so you could try
Dimension dimension = new Dimension(800, 600);
driver.manage().window().setSize(dimension)
For Responsive Design it's better if you don't use fixed data, in this case screen resolution - users need most often (cause it's much less prone to errors) :
_driver.manage().window().maximize();
or
_driver.manage().window().fullScreen();
If nothing works (to make tab in full screen only) for you, go for it:
driver.maximize_window()
where
driver = webdriver.Chrome(executable_path="D:\softwares\chromedriver.exe")
or could be any other webdriver locations.
We can also use Chrome capability to resize the window.
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=800,480");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);

Categories

Resources