How dismiss ALERT popup while opening a browser - java

I open a browser:
'''
https://portal-qa1.maytronics.co/#/login/login
After successfully logging in (user: Shoval.Ziman#comm-it.com, pass: 123456),
I go to the page I need and a ALERT message appears.
I tried to remove it in many ways, nothing comes out.
The switchTo() method - doesn't work.
The Robot(), doesn't work two:
Robot alertAllow = new Robot();
alertAllow.mouseMove(280, 160);
alertAllow.mousePress(InputEvent.BUTTON1_DOWN_MASK);
alertAllow.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(3000);
Also I tried to use the next method:
WebDriverManager.chromedriver().setup();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
and that didn't help either.

Try this:
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_ENTER);
// or
robot.keyPress(KeyEvent.VK_ESCAPE);
Usually, after hitting enter or escape most alerts go away.

Related

Chromeoptions and setExperimentalOption code

I am unable to understand the meaning of following lines of code for setting up Chromeoptions in selenium code
Can someone explain its meaning and als provide some external link for further learning -:
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
prefs.put("profile.default_content_setting_values.notifications", 2);
options.setExperimentalOption("prefs", prefs);
Any help on this issue will be highly appreciated.
Here is the complete details :
ChromeOptions options = new ChromeOptions();
Through this line you are creating an object by the name options of ChromeOptions Class.
Map<String, Object> prefs = new HashMap<String, Object>();
Here you have created a new Map object by the name prefs where the Key and Value fields accepts String and Object type of data and casted it to HashMap.
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
prefs.put("profile.default_content_setting_values.notifications", 2);
In these three lines you have configured the pref-names within the prefs object.
options.setExperimentalOption("prefs", prefs);
Finally in this line you are using the setExperimentalOption method to set these experimental options (ChromeDriver options not yet exposed through the ChromeOptions API) within the options object.
Now you can use this options object of ChromeOptions Class to initialize the WebDriver and Web Client as follows :
WebDriver driver = new ChromeDriver(options);
These are chrome browser preferences. You can set using options. You can find full list here in source code of chromium
https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup

How to enable or disable geolocation by selenium test case

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

Focus on recent open tab using selenium webdriver

I have one website in which When I click on button, it open new tab with link in same browser.
I want to tell selenium to focus on that recently open tab.
I tried many methods but none of them seem helpful in my case.
I have tried :
Method 1 :
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.TAB);
Method 2 :
((JavascriptExecutor) webDriver).executeScript("window.focus();");
Method 3:
driver.switchTo().window(driver.getWindowHandles().last());
Try this code, use Java Robot. that worked for me.
ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
System.out.println(tabs2.size());
for (int i = tabs2.size()-1; i>=0; i--) {
Thread.sleep(2000);
driver.switchTo().window(tabs2.get(i));
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
System.out.println(driver.getTitle() + "i: " + i);
// do what you needed
}

How can Selenium close Chrome browser and open new One

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.

Selenium Internet Explorer click button

I have a problem. I want to click for button and open link in new tab. This my code:
Actions actions = new Actions(this.driver);
actions.keyDown(Keys.CONTROL).click(myButton).keyUp(Keys.CONTROL).perform();
But link is opened on current tab, not new tab.
This is bug of that?
P.S. on chrome and firefox this code work very well
One solution would be to open a new window before clicking the link :
WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
// open a new window
((JavascriptExecutor)driver).executeScript("window.open(window.location.href, '_blank');");
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);
// click the link
driver.findElement(By.linkText("Stack Overflow")).click();
Another solution would be to set the target attribute on the targeted element:
WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
WebElement element = driver.findElement(By.linkText("Stack Overflow"));
// set the target _blank on the link
((JavascriptExecutor)driver).executeScript("arguments[0].target='_blank';", element);
// click the link
element.click();
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);
Your code is fine but the IEDriver is not quite as good as the ones for FireFox and Chrome. I had similar issues, as you can read here. I ended up solving my issues with the java.awt.Robot. In your case that would mean you'd need an IE-specific method for opening in new tab.
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
...
r.setAutoDelay(30);
r.keyPress(KeyEvent.VK_CONTROL);
//then click the button
r.keyRelease(KeyEvent.VK_CONTROL);
It's not elegant but it works.

Categories

Resources