Hi I am trying to add some settings to my driver in selenium but can seem to make them work.
Im trying to add
{
"DEVELOPER_MODE":false,
"AUTOMATION_MODE":true,
"AUTOMATION_LAUNCH_URL":"{whatever_url_you_want_to_test_with}"
}
to my driver file
public static WebDriver startDriverTwo() {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.addArguments("start-maximized");
opt.addArguments("disable-infobars");
opt.addArguments("--disable-extensions");
opt.addArguments("--window-size=1920x1080");
opt.addArguments("--disable-cache");
//options.addArguments("--headless");
opt.addArguments("--disable-application-cache");
opt.addArguments("--disk-cache-size=0");
opt.addArguments("--disable-gpu"); // applicable to windows os only
opt.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
opt.addArguments("--dns-prefetch-disable");
opt.addArguments("--disable-notifications");
opt.addArguments("disable-infobars")
//Enter the path of your Electron app
opt.setBinary("pathtoelectronapp.exe");
driver = new ChromeDriver(opt);
System.out.println("opening app");
return driver;
}
I have tried
setExperimentalOption()
setProperty()
But neither of these work? any idea how i can work these in?
DEVELOPER_MODE - you got that right, it's opt.addArguments("--disable-extensions");
I'm little bit confused with these settings, since:
AUTOMATION_MODE - isn't this always true, when using Selenium?
AUTOMATION_LAUNCH_URL - you mean driver.get(URL) ?
Related
Is there a way to get selenium screenshots with headers ? I've tried the code below but the screenshot does not have a header.
I have a test case that requires clicking a link and making sure the action must bring to a new tab, so as evidence I have to attach capture there are two tabs.
public static void main (String args[]) throws IOException {
DesiredCapabilities dc = new DesiredCapabilities();
RemoteWebDriver driver;
URL url = new URL("http://localhost:4444/wd/hub");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
dc.setCapability(CapabilityType.PLATFORM, "MAC");
driver = new RemoteWebDriver(url, dc);
driver.manage().window().maximize();
driver.get("https://google.com");
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
File getImage = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(getImage, new File("/Users/path/screenshot.jpg"));
driver.quit();
}
Current result
Expected result
No you can't, the screenshot functionality in Selenium takes an image of the rendered DOM. The browser chrome (i.e. the the UI components of the browser rendered by the OS the browser is running on) is not part of the DOM so Selenium is unaware of it.
The next question is why do you want the browser chrome in your image? If you are just trying to find out the displayed URL (as your question implies) you can use the command driver.getCurrentUrl(); instead.
As #Ardesco suggested, it is not possible to take screenshot.
However i think you can use java.awt.Robot class to capture the screen. It takes the screenshot of the current screen.
Here's an snapshot of code for capturing screenshot using java.awt,
public void getScreenshot(int timeToWait) throws Exception {
Rectangle rec = new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(rectangle);
ImageIO.write(img, "jpg", setupFileNamePath());
}
I'm using Firefox browser and i want to set the zoom level 90% while it executing the script.
I have tried to set using JavascriptExecutor like -
((JavascriptExecutor)driver).executeScript("document.body.style.transform='scale(0.9)'");
Its working for specific command lets say in my Listeners file i have place this if its a get command. it resize the browser after get URL and then it restored back to default once another command getting executed.
I'm looking for the solution like DesiredCapabilities of things so there i can add the zoom level for the browser.
FirefoxProfile profile= new FirefoxProfile();
profile.setPreference( "layout.css.devPixelsPerPx", "0.9" );
WebDriver driver = new FirefoxDriver(profile);
The above will set the firefox profile preference and simulate a zoom level of 90% for 110% set it to 1.1
How about something like this one -
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class A {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
Dimension dMax = driver.manage().window().getSize();
int mHeight = (int) (dMax.height *.9);
int mWidth = (int)(dMax.width *.9);
Dimension d = new Dimension(mWidth, mHeight);
driver.manage().window().setSize(d);
System.out.println( driver.manage().window().getSize());
}
}
There is not the capability level option is available in Firefox.
But screen size can be increased or decrease by webdriver:
driver.manage().window().setSize(value);
I want to allow/block my current location accessible to site by clicking on allow button of that popUp, my chrome version is 62.0, chrome driver version is 3.6.0 and I am using ubuntu 16.04 and my code snippet is,
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.addArguments("start-maximized");
options.addArguments("--disable-geolocation");
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
driver = new ChromeDriver(capabilities);
But this is not working, could anyone suggest me the perfect solution for this?
Robot r = new Robot();
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Use java.awt.Robot class for this, first test manually and then you can change the key handlers as needed.
For those looking for a way to do it without out robot, you can do it with options:
To deny set profile.default_content_setting_values.geolocation to 1, to auto accept set to 2 (which seems to the current default)
ChromeOptions options = new ChromeOptions();
ArrayList<String> opArgList = new ArrayList<>(); // using an array list (so we can extend it with other passed in options)
opArgList.add("--no-sandbox");
opArgList.add("--disable-dev-shm-usage");
String[] opArg = opArgList.toArray(new String[0]);
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.geolocation", 1);
options.setExperimentalOption("prefs", prefs);
options.addArguments(opArg);
driver = new RemoteWebDriver(new URL("http://localhost:4444/"), options);
I've tried a different options just to open new tab but all of them lead to the same result : sending char "t" to google search field.
The goal in my real test is to switch between tabs in browser, but I am unable even open new one.
Very simple test
public class LoginPhp2 {
#Test
public void testGoogle() {
WebDriver driver = new SafariDriver();
driver.get("https://www.google.com");
//driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "t");
Actions action= new Actions(driver);
action.keyDown(Keys.COMMAND).sendKeys("t").build().perform();
//action.keyDown(Keys.COMMAND).sendKeys("t").keyUp(Keys.COMMAND).build().perform();
}
}
You may use javascript to open a new tab.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open();");
There are different ways of opening new tabs in windows and Mac. Actual keys to send depend on your OS, for example, Mac uses COMMAND + t, instead of CONTROL + t.
You can open new tab in Mac using:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND +"t");
After this you can switch to any of the opened tabs:
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
I have followed the approach suggested earlier:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open();");
This is opening new tab in the same window. I have tried it on my MAC machine
My scenario is to close the chrome browser and open a new one.
public String openNewBrowserWindow() {
this.log("Opening new Browser window...");
String stringHandles;
Set<String> previousWindows = driver.getWindowHandles();
String previousHandle = driver.getWindowHandle();
((JavascriptExecutor)driver).executeScript("window.open();");
Set<String> newWindows = driver.getWindowHandles();
newWindows.removeAll(previousWindows);
String newHandle = ((String)newWindows.toArray()[0]);
stringHandles = previousHandle + ";" + newHandle;
return stringHandles;
}
What I did is this:
String handlesA = generic.openNewBrowserWindow();
String[] handleA = handlesA.split(";");
generic.closeBrowser();
generic.switchToWindow(handleA[1]);
This works on firefox but not in chrome. Do you guys have any suggestion?
Why not just:
driver.quit()
driver = new ChromeDriver()
What is your scenario really?
#Seimone
Whenever you want to intiate a Chrome browser, system property must be defined to the chromedriver.exe
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
WebDriver driver = new ChromeDriver();
Also, If you want to close your current chrome browser window use the following one in your code.
driver.close();
If you want to close all your chrome browser window use the following one in your code.
driver.quit();
With reference to your scenario
Open the url
Login with signed in
Close the browser
Open the browser and enter the same url
Check the same user is logged in
Try the below code and let me know your result.
String chromeDriver = "enter the chromedriver.exe path";
String chromeProfile = "C:/Users/MSTEMP/AppData/Local/Google/Chrome/User Data/Default"; //Local chrome profile path.
System.setProperty("webdriver.chrome.driver", chromeDriver);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir="+chromeProfile);
capabilities.setCapability("chrome.binary",chromeDriver);
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.gmail.com");
/*write your login credentials code with username, password and perform the
login operation with signed in*/
driver.close();
//Now invoke the chrome browser and enter the url alone.
driver = new ChromeDriver(capabilities);
driver.get("http://www.gmail.com");
//write the code for user signed verification.