alert window in Selenium 2

619 views
Skip to first unread message

pmarek

unread,
May 31, 2011, 8:57:10 AM5/31/11
to webdriver
Hi all , I have question about alert window. I would like check if the
text on the alert window is correct. I have following part of code;

_driver.Navigate().GoToUrl("http://page");
IWebElement inafns = _driver.FindElement(By.Id("sc_Infants"));
inafns.SendKeys("9");
inafns.Clear();

Alert window: http://screencast.com/t/1BbjlnrDSH

How I can check that text on the screenshot is correct? I don't know
how I can do it, I've checked a hundreds pages but without success.



Maybe anyone knows how can I do it?

Derek Ekins

unread,
May 31, 2011, 10:06:05 AM5/31/11
to webd...@googlegroups.com
something like:

driver.switch_to().alert().getText()

depending on the language you are using..


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


pmarek

unread,
May 31, 2011, 10:38:24 AM5/31/11
to webdriver
Hi Derek, thanks for you replay. I've also found on other forum such
solution as yours, in my case it looks as below (I'm using C# and
Nunit):

driver.SwitchTo().Alert().Text.Contains("The number of infants entered
is higher than the number of adults. It will be reduced to the maximum
allowed for the given adults number.");

However it seems to me that this is not quite correct, because when I
changed the string in apostrophes (I've have something link:
Text.Contains("qqqqqq")) the script also passed my script, but in this
case shouldn't. So therefore I'm looking another solution, or maybe
iI'm doing something not correct?



On May 31, 3:06 pm, Derek Ekins <de...@spathi.com> wrote:
> something like:
>
> driver.switch_to().alert().getText()
>
> depending on the language you are using..
>

Derek Ekins

unread,
Jun 1, 2011, 5:58:37 AM6/1/11
to webd...@googlegroups.com
If it is not working then I would raise a bug for it - http://code.google.com/p/selenium/issues/entry
I don't use the c# driver so am not sure of its current state

pmarek

unread,
Jun 2, 2011, 10:39:37 AM6/2/11
to webdriver
So, there is no possibility check that alert with Selenium 2 (C#/
NUnit)?

On Jun 1, 10:58 am, Derek Ekins <de...@spathi.com> wrote:
> If it is not working then I would raise a bug for it -http://code.google.com/p/selenium/issues/entry
> I don't use the c# driver so am not sure of its current state
>

Jim Evans

unread,
Jun 2, 2011, 3:22:25 PM6/2/11
to webdriver
Your C# code looks correct. I'd log what's being returned by
IAlert.Text, then see if you can figure out why the .Contains() is
failing. Rather than doing it all in one statement, I'd do something
like this:

string alertText = driver.SwitchTo().Alert().Text;
Console.WriteLine(alertText);
Assert.IsTrue(alertText.Contains("qqqqqq"));

--Jim
> > >http://groups.google.com/group/webdriver?hl=en.- Hide quoted text -
>
> - Show quoted text -

pmarek

unread,
Jun 3, 2011, 10:21:48 AM6/3/11
to webdriver
Hi Jim,

thanks for your response, when I used your script the windows handle
is working correctly.

What is more interesting when I was trying find solution for my
problem I found IAlert interface and I used it to my case:

IAlert alert = driver.SwitchTo().Alert();
alert.Text.Contains(
"The number of infants entered is higher than the number of adults. It
will be reduced to the maximum allowed for the given adults number.");
alert.Accept();

But I had also problem with this, because as a results I had
information:

'Unexpected error. Unable to dismiss alert. Is an alert present?'

What do you think bout it? IAlert interface is not correct solution to
windows handle?


@ Derek - thanks also for your information, they were also useful for
me.

Cheers,

Pawel
> > > >http://groups.google.com/group/webdriver?hl=en.-Hide quoted text -

Jim Evans

unread,
Jun 3, 2011, 3:22:29 PM6/3/11
to webdriver
Are you using the latest Selenium WebDriver release (2.0rc2)? What
browser are you seeing this behaviour in? Does it happen in other
browsers?

--Jim
> > > > >http://groups.google.com/group/webdriver?hl=en.-Hidequoted text -
>
> > > - Show quoted text -- Hide quoted text -

pmarek

unread,
Jun 3, 2011, 5:11:33 PM6/3/11
to webdriver
I'm using Firefox 4.0.1, with Nunit 2.5.10, VS2008, and Selenium
WebDriver 2.0b3.

Do you think that it could be a bug in Selenium?

Pawel
> > > > > >http://groups.google.com/group/webdriver?hl=en.-Hidequotedtext -

Jim Evans

unread,
Jun 3, 2011, 5:20:51 PM6/3/11
to webdriver
I know there were lots of changes specific to Firefox 4 that were
added after 2.0b3. You should try it with the updated 2.0rc2.

Pawel Marek

unread,
Jun 6, 2011, 9:35:56 AM6/6/11
to webdriver
Hi Jim,


I've installed the latest version of Selenium RC and the below code:

IAlert alert = driver.SwitchTo().Alert();
alert.Text.Contains("The number of infants entered is higher than the
number of adults. It will be reduced to the maximum allowed for the
given adults number.");
alert.Accept();

works good right now - good means that I don't have error: 'Unexpected
error. Unable to dismiss alert. Is an alert present?'.


But it seems to me that there is a different issue related with it, I
mean: now when I change text in line e.g.:
alert.Text.Contains("dedede") the test is also passed. I'm not sure
what is going here?

I need to verify text on alert window and then if is correctly click
on 'OK' button.


Pawel

Bill Ross

unread,
Jun 6, 2011, 12:48:47 PM6/6/11
to webd...@googlegroups.com
> IAlert alert = driver.SwitchTo().Alert();
> alert.Text.Contains("The number of infants entered is higher than the
> number of adults. It will be reduced to the maximum allowed for the
> given adults number.");
> alert.Accept();

My guess is that Contains is returning a boolean, but you are not
looking at it, e.g. by wrapping it in an assertion, so no test is
actually taking place.

Bill

Message has been deleted

Pawel Marek

unread,
Jun 7, 2011, 5:01:10 AM6/7/11
to webdriver
Hi Bill,

thanks for your tips, I had to add only Assert.AreEqual to my script:

IAlert alert = driver.SwitchTo().Alert();
Assert.AreEqual("text", alert.Text);

Cheers,
Pawel
Reply all
Reply to author
Forward
0 new messages