Unable to select IE8 popup window using selenium RC - selenium

I am trying to send commands to a popup window in IE8 with Selenium RC.
Currently, I am able to click on a button and open the popup window.
However, I am unable to select the window to send commands.
I have done extensive research on this subject for about a week and I believe this is strictly an IE issue. I have found some work-arounds; several blogs have instructed users to open up a blank window to "catch" the commands.
However, when I implement the code below, a blank window pops up and upon clicking the button that opens the pop up, a THIRD window opens and loads with the page I need.
Am I setting the target inaccurately? Please see below for selenium rc commands as well as the html source code of the page.
Java Code
selenium.runScript("selenium.browserbot.document.getElementByID(
'ctl00_ContentPlaceHolder1_WebGroupBox1_btnAddRequirement').target='popup'"
);
selenium.openWindow("", "popup");
///Click on Add Requirement button
selenium.click("id=ctl00_ContentPlaceHolder1_WebGroupBox1_btnAddRequirement");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
selenium.selectWindow("popup");
HTML for Add Requirement button
<input id="ctl00_ContentPlaceHolder1_WebGroupBox1_btnAddRequirement"
type="submit"
style="font-family:Arial,Times New Roman,Courier New;"
onclick="return validateProject();
WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(
"ctl00$ContentPlaceHolder1$WebGroupBox1$btnAddRequirement",
"",
true,
"",
"",
false,
false
)
)
"
value="Add New Requirement"
name="ctl00$ContentPlaceHolder1$WebGroupBox1$btnAddRequirement"
>

In WatiN (different I know), I had to send a combination of alt+tab and enter to deal with pop-up boxes.

Related

How to handle the window modal dialog box in Internet Explorer using selenium Webdriver

I am trying to automate a scenario where I am encountering a window modal dialog box. Please let me know how to automate this situation? I just want to know how to click the highlighted OK button on the popup appearing? Please suggest
Try this
driver.switchTo().alert().accept();
You can also send keyboard events to press enter key as soon as pop up is active
Pressing enter key is as equivalent as clicking OK button
Make use of Robot class in java
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER)
You can try this by using JavascriptExecutor. It always works if we fail to find element using findelement method
I just found a way to handle this issue.
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
d = new FirefoxDriver(dc);
then implemented the alert code in try catch block
try
{
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println("Alert data: " + alertText);
alert.accept();
}
catch (UnhandledAlertException e)
{
e.printStackTrace();
}

Facing issues in Alertbox selenium 3.0

After clicking save button a pop will open and i write a code below to do accept but its not working.
`driver.findElement(By.id("save")).click();
Alert succ=driver.switchTo().alert();
System.out.println(succ.getText());
Thread.sleep(2000);
succ.accept();`
Need Help please.
The behavior of selenium varies according to browser, In some case the actions done in Firefox browser like form filling actions or clicking the button is faster then Chrome browser so your script well executed in chrome but throw error in Firefox so you need to add some pause to make them perform well.
So in your case after clicking the save button selenium executing the command too fast so skip to switch on alert and accept so add some wait using Thread.sleep(); to make pause
driver.findElement(By.id("save")).click();
Thread.sleep(2000);
Alert succ=driver.switchTo().alert();
System.out.println(succ.getText());
Thread.sleep(2000);
succ.accept();
Note : It is not recommended to use Thread.sleep(); instead use implicit or explicitwait conditions
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.alertIsPresent());
Try this:
driver.findElement(By.id("save")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert().accept();

Watin AttachTo popup window

We are using Watin to automate the website testing and try to implement the following: Click on a button to bring up popup, click on the Generate button on the popup window to generate file. We wasnt able to get the popup window using watin. ERROR: "Could not find an IE window matching constraint: Attribute 'href' matches 'Regenerate'. Search expired after '30' seconds." Please give a hint on what has gone wrong. Your help would be much appreciated.
The code snippet as below:
ieInstance.Button(Find.ByName(Button1)).Click();
ieInstance.WaitForComplete();
var popupUrl = new Regex("Regenerate");
//part of popup URL: "RegenerateDialog.aspx?Type="
var popupBrowser = Browser.AttachTo<IE>(Find.ByUrl(popupUrl));
if (popupBrowser != null)
popupBrowser.Button(Find.ById("RegenerateDialogBtn")).Click();
The popup window code as below:
function ShowEXPFileWindow(Type)
{
//do some stuff
var oWnd = window.radopen("RegenerateDialog.aspx?Type=" + Type, "RegenerateDialog");
oWnd.set_title("");
oWnd.center();
}
HTML to bring up popup window:
<td><input type="submit" name="ctl00$Body$ConfigRadGrid" value="Regenerate" onclick="ShowEXPFileWindow('OD'); return false;" class="submit" /></td>
Instead of
ieInstance.Button(Find.ByName(Button1)).Click(), use
ieInstance.Button(Find.ByName(Button1)).ClickNoWait()
and check. Is it possible to post the image of the pop-up window? or when you click on button, which control is selected by default? If it is 'Regenerate' then try to press 'Enter' Key and check.
Is the pop window is another IE window? If yes, then attach it using
Find.ByUrl(url => url.Contains(expectedURL))

How to do a forced page refresh using selenium webdriver?

In my automation, at one point I have to refresh the page to get the updated page content. But during the refresh the webpage is asking for a confirmation to resend the data (basically an alert is displayed on the page).
Even though I switched the focus to the alert and accepted it, the page contents are not getting refreshed. If I manually do the same, page contents are getting refreshed.
Is there any alternative way to refresh the page using Selenium Webdriver apart from navigate().refresh() command?
Or is there any way I can click on the Retry button on the alert without accepting the alert??
In ruby try using this - #driver.switch_to.alert.accept
this will click on Resend
java code driver.switchTo().alert().accept();
Refreshing Page in Selenium Webdriver using Java:
public boolean refresh()
{
boolean executedActionStatus = false;
try{
((JavascriptExecutor)driver).executeScript("document.location.reload()");
Thread.sleep(2000);
executedActionStatus = true;
}
catch(Exception er)
{
er.printStackTrace();
getScreenShot(method_TableName, "ExpCaseShot");
log.error(er);
}
return executedActionStatus;
}

cant click button which opens file attachment dialog

i'm usinng selenium 2 beta. i'm trying to click button which opens file attachment dialog. but when i click it nothing happens.
<input class="zf" name="Passport" id="PassportUpload" type="file" onclick="return { oRequired : {} }" maxlength="524288">
driver.findElement(By.name("Passport")).click();
using just selenium not selenium 2 i can click it easily.
I guess that the issue is only when using Internet Explorer since IE and FF handles the File Input slightly different: in FF you can click on the button or the field to invoke the Open dialog, while in IE you can click on the button or double-click on the field.
WebDriver using native events so it is sending a native mouse click to the File Input control which is translated to the click on the input field.
It was working in Selenium 1 because it is using the JavaScript to fire the events. To make it work in WebDriver you need to invoke the JavaScript:
WebElement upload = driver.findElement(By.name("Passport"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
However the code abouve will not in Firefox, so you can use something like:
WebElement upload = driver.findElement(By.name("Passport"));
if (driver instanceof InternetExplorerDriver) {
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
} else {
upload.click();
}
maybe try following code:
WebElement upload = driver.findElement(By.name("Passport"));
if (driver instanceof InternetExplorerDriver) {
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
} else if (driver instanceof FirefoxDriver) {
((JavascriptExecutor)driver).executeScript("arguments[0].click;", upload);
}else {
upload.click();
}