Chromedriver: ChromeDriver: new Augmenter().augment( driver ) leads to new browser window - java

In order to take screen shots we have to augment driver with screen shot feature in following way:
driver = new Augmenter().augment(driver);
But when we run this code in Chrome it opens new empty window. How can I avoid it?
Here is my code:
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.Augmenter;
public class NewChromeTest {
public static void main(String args[]) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.ua/");
driver = new Augmenter().augment( driver );
}
}
What steps will reproduce the problem?
1. Run code above
What is the expected output?
We should stay in the same browser window.
What do you see instead?
New empty browser (Chrome) window opens
Selenium version: 2.37
OS: Win 7
Browser: Chrome
Browser version: 31.0.1650.63 m!
screenshot

Try adding the below two lines to your code
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("D:\\screenshot.png"));

Try this.
driver= new Augmenter().augment(driver);
File scrFile =(File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(directory.getCanonicalFile()+"\\Images\\LatestFailedScreenshot.png"));

Related

How to use tor browser using selenium webdriver (java)? I have tried below code so far but getting message: 'tor failed to start'

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class torr1 {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette",
"C:\\Users\\ghorh\\Documents\\selenium-bazinga\\Drivers\\geckodriver.exe");
String torPath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\ghorh\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
WebDriver driver = new FirefoxDriver(options);
driver.get("http://google.co.in");
}
}
I have tried the above code so far but getting message: 'tor failed to start'. Could somebody please help on what changes are required for the above code.
You are trying to use FireFox driver.
Try use TorBrowserDriver as specified in its readme: https://github.com/webfp/tor-browser-selenium
Or if you want to use firefox - use GeckoDriver that can be downloaded from the next link:
https://github.com/mozilla/geckodriver/releases/tag/v0.26.0

How to take full page screenshot using AShot library through Selenium and Java

I tried the below code for taking full page screenshot. But only the visible area is captured,
public void Fullscreen (WebDriver driver)
{
try {
final Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
final BufferedImage image = screenshot.getImage();
ImageIO.write(image, "PNG", new File("D:\\" + "AShot_BBC_Entire.png"));
} catch(Exception e){
System.out.println(e.getMessage());
}
}
While working with Selenium Java Client v3.14.0, ChromeDriver v2.41, Chrome v 68.0 using ashot-1.4.4.jar here is an example to take the complete page screenshot both horizontally and vertically using ChromeDriver and aShot Library of the url https://jquery.com/:
Code Block:
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ashot_CompletePage {
public static void main(String[] args) throws Exception {
System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://jquery.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
driver.quit();
}
}
Screenshots:
Reference
You can find a detailed discussion in How to take screenshot with Selenium WebDriver
Want to add answer in case when you don't know what kind of screen is used. (retina or not)
In this case you need find devicePixelRatio of browser window:
Object output = ((JavascriptExecutor) webDriver).executeScript("return window.devicePixelRatio");
String value = String.valueOf(output);
float windowDPR = Float.parseFloat(value);
Then you can use ShootingStrategy with scaling;
ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR), 100)

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();

Selenium Java New Tab in Chrome - won't open , keeps opening URL`s in same tab

package javaapplication1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class JavaApplication1 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Aca\\desktop\\chromedriver.exe");
// Initialize driver
WebDriver driver = new ChromeDriver();
//Maximize browser window
driver.manage().window().maximize();
//Go to URL
driver.get("http://www.google.com");
//Set timeout
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Open new tab – May be you are stuck here
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
//Go to URL
driver.get("http://www.gmail.com");
//Set new tab timeout
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
I am trying to open a new tab, leaving the previous tab opened ..
I can't get a new tab opened. It keeps opening URL`s in the same tab.. I also tried using Actions.
You need to switch the driver to the new tab. In Chrome its done like switching windows
String tabHandle = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(tabHandle))
{
driver.switchTo().window(handle);
}
}
driver.get("http://www.gmail.com");
// close the new tab and switch back to the old one
driver.close();
driver.switchTo().window(tabHandle);
As a side note, implicitlyWait is defined for the driver, not tab/window. No need to define it again after opening the new tab.
This is an issue with chromedriver itself. See the related bug submitted
Please try this to open new tab in Chrome with Selenium Java.
package com.sample;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class NewWindow {
public static void main (String[] args){
System.setProperty ("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-arguments");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
}
}
You are forgetting to change the focus of the driver to the new tab before navigating.
Try this after opening a new tab:
driver.sendKeys(Keys.chord(Keys.CONTROL,"t"));
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); //Get tabs
driver.switchTo().window(tabs.get(1)); //Navigate to new tab
driver.get("http://www.gmail.com"); //Navigate within new tab
driver.close(); //Close the tab when done
driver.switchTo().window(tabs.get(0)); //Navigate to original tab

Categories