How to switch focus from WinAppDriver to IWebDriver? - selenium

I am working on Desktop application using WinAppDriver and Selenium C#.
There are couple of links on the application.
If we click on the link, it will redirect to default browser.
How I can switch focus from WinAppDriver to IWebDriver ?
How to verify the link, whether the link opened in default browser or not?
Please help on this. Thank you.

// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
// Close the new window, if that window no more required
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);

Related

Maximize chrome window in QAF

I am using this code,
new WebDriverTestBase().getDriver().get("http://www.google.com/");
But this doesn't launch the chrome in full screen. How to maximize the chrome window in QAF?
To maximize web window,
getDriver().manage().window().maximize();
or
getDriver().manage().window().fullscreen();
or
getDriver().manage().window().setSize(new Dimension(width, height));
For chrome you can try using chromeOptions capability
chrome.additional.capabilities={"chromeOptions":{"args":["--start-maximized"]}}
Above solution is chrome browser specific. In order for solution to work with different browsers, you also can implement driver listener and maximize window on driver initialize. For example, your listener method can look like below:
package com.sample;
....
public class WindowMaximizeListener extends QAFWebDriverCommandAdapter {
#Override
public void onInitialize(QAFExtendedWebDriver driver){
//write code to maximize browser window
driver.manage().window().maximize();
}
}
Register listener using qaf.listeners property.
qaf.listeners=com.sample.WindowMaximizeListener
Above listener will maximize browser window as and when new browser session created.

How can switch Windows pop-up in Internet Explorer in Selenium Webdriver using JAVA?

I am unable to switch windows pop-up in internet explorer. I checked the security setting there intranet setting is not enable mode and we don't have admin access to change. Can any one help me on this
String handle= driver.getWindowHandle();
System.out.println(handle);
driver.findElement(By.name("New Message Window")).click();
Set handles = driver.getWindowHandles();
System.out.println(handles);
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
System.out.println(driver.getTitle()); //Trying to print Title here
}

switch between tabs in chrome using arquillian graphene

I need to switch tabs using Graphene and continue the test without failing.
I am testing an application that creates a project and then opens that project from a link on a page.
My issue is that it opens the project in a new tab and to continue the test I have to switch to the new tab.
I am using the selenium switch windowHandler but getting an error from the Graphene. I will not know the new URL until the project has been created.
//Switch to tab opened from previous test
public void switchTab(int tabNumber) throws Exception{
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
Thread.sleep(3000);
}
}
Error: The page object 'CreateChangeSubProjStruct' that you are navigating to using either Graphene.goTo() or #InitialPage is not annotated with #Location
I can see the new page opens and is on top but the test is running on the original tab.

Can we handle the chrome alert which appears while automating the webpage using selenium?

Please follow the below mentioned steps:
1.) go to this website : http://demo.guru99.com/V4/index.php
2.)punch in any junk login id and password , and then the dialog box appears says:
USER OR PASSWORD IS NOT VALID
3.)Q1: Is there any way I can punch in the "ok" button in this dialog box ???because I can't inspect the element of this dialog box.
4.)Q2: When u try to login to gmail [ password shouldn't be saved], after punching the login details, it asks, do you want to save this password for this site ? Can this popup be handled ? because here also I can't inspect any information .
Q1: Selenium can interact with javascript (browser based) alerts using the Alert interface.
You can try any of the following:
using the explicit wait
Wait<WebDriver> wait = new FluentWait<>(driver).withTimeout(5, TimeUnit.SECONDS).pollingEvery(500,msecs).ignoring(NoSuchElementException.class
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
directly getting the alert
driver.switchTo().alert().accept();
using webdriver / chromedriver capabilities (to handle unexpected alert exception)
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
Q2: I'm unable to replicate the save password popup for gmail. However, you should be able to do it by using .dismiss(); instead of .accept();.
Note: If you're using google chrome for testing, be reminded that chrome usually updates automatically. Selenium alert interaction may not work all of a sudden if it is not compatible with the latest chrome version. With this, I suggest you keep your chromedriver exe and java bindings updated.
Q1: Is there any way I can punch in the "ok" button in this dialog box ???because I can't inspect the element of this dialog box.
Answer: For scenarios where you need to handle an alert, you can write a generic method similar to the one mentioned below, and pass the appropriate arguments while invoking/calling it:
public static void handleAlert(WebDriver driver, boolean accept) {
Alert popup = driver.switchTo().alert();
if (accept)
popup.accept();
else
popup.dismiss();
}
While calling above method, you can pass true, when you want to accept the alert and false when you do not want to accept it.
Q2: When u try to login to gmail [ password shouldn't be saved], after punching the login details, it asks, do you want to save this password for this site ? Can this popup be handled ? because here also I can't inspect any information .
Answer: Solution in this scenario will depend on which browser is being used. As, you are using Chrome browser, you can use following to code to handle the required pop-up:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches", "--disable-extensions --disable-extensions-file-access-check --disable-extensions-http-throttling --disable-infobars --enable-automation --start-maximized");
options.setExperimentalOption("credentials_enable_service", false);
options.setExperimentalOption("profile.password_manager_enabled", false);
WebDriver driver = new ChromeDriver(options);
Let me know, if you have any further queries or above solutions do not work for you.
Try using JavascriptExecutor to accept alert:
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
Alert alert=driver.switchTo().alert();
alert.accept();
You can accept the alert box pop out as follows.
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "C:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();

Windows Internet Explorer Modal dialog box preventing selenium test run

I am trying to execute test cases in IE 8. But sometimes I am getting Windows Internet Explorer Security Warning "Do you want to enable the security content for this page" or Modal dialog box "Stop running this script...". How do I handle it in selenium Webdriver. Any help will be greatly appreciated.
There is an option in InternetExplorerOptions which gives you the opportunity to automatically close the modal if unexpected.
This is how I handle in C# and this should be fairly similar in Java or other bindings as well.
var options = new InternetExplorerOptions { EnableNativeEvents = false };
options.EnsureCleanSession = true;
//This is the main hook
options.AddAdditionalCapability("disable-popup-blocking", true);
Driver = new InternetExplorerDriver(options);