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()
Related
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);
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);
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))
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);
Using Selenium Webdriver 2. java.
I would like to switch back in forth between two firefox browser windows. When I do I get: org.openqa.selenium.NoSuchWindoException: Unable to loacate window"{accb1cc2-74c9-3b4e-8f71-c0b184a037c4}"; duration or timeout:
Here is the java:
driver = new FirefoxDriver();
driver.get("http://mail.google.com");
String firstWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+firstWindowHandle);
Thread.sleep(1000);
driver = new FirefoxDriver();
driver.get("http://www.google.com");
// Get names of currently open windows
String secondWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+secondWindowHandle);
Thread.sleep(1000);
// It fails right here!
driver.switchTo().window(firstWindowHandle );
driver.get("http://www.lifehacker.com");
It prints the following to the console:
- handle of first window = {accb1cc2-74c9-3b4e-8f71-c0b184a037c4}
- handle of the second window = {f5256619-a36e-a441-9979-937da0abacd1}
All help is appreciated.
Unfortunately, you cannot switch between windows the way you are currently trying to do it - WebDriver lost the first window as soon as you instantiated a new instance.
You could try opening the second window via javascript and then switching back and forth from it:
window.open('http://www.bing.com','Bing','modal=yes,alwaysRaised=yes')
This is a bit of a hack, and could have the following problems:
Popup blockers may prevent the action
The browser must have javascript enabled
Future browser versions may break the hack
Complaining and murmuring from peers (and perhaps rightly so) because even though it might work, it's still a hack ;)
Some final thoughts:
Is there any particular reason it has to be the same driver instance?
If not, just switch between two driver instances:
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com");
FirefoxDriver driver2 = new FirefoxDriver();
driver2.get("http://www.google.com");
Swtiching between 2 active Windows:
FirefoxDriver wd=new FirefoxDriver();
wd.get("https://irctc.co.in/");
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);
WebElement wb=wd.findElement(By.linkText("Cabs"));
wb.click(); //Now 2 Windows are open
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS); //Wait for the complete page to load
Set<String> sid=wd.getWindowHandles(); //getWindowHandles() method returns the ids of all active Windows and its return type will be a Collection Set.
Iterator<String> it=sid.iterator(); //Using iterator we can fetch the values from Set.
String parentId=it.next();
System.out.println(parentId);
String childId=it.next();
System.out.println(childId);
wd.switchTo().window(childId); //swtiching control to child Window
wd.close(); //control returns to the parent Window.