Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Using WebDriver and JUnit 4.11. I'm attempting to find an alert and assert the body of the alert paragraph.

My code is:

@Test
public void dNameField(){

    WebElement nameField = chrome.findElementByCssSelector("input.t-textInput-A[name='name']");
    nameField.clear();

    List<WebElement> elements;
    elements = chrome.findElements(By.className("logMessage.error"));

    Set<String> foundAlerts = new HashSet<String>();

    for(WebElement e : elements){
        foundAlerts.add(e.getTagName());
    }

    Assert.assertTrue("This should be an error message", foundAlerts.contains("An error has occurred : Value must be a Number."));

I receive a java.lang.AssertionError: in the stacktrace.

In the DOM before an alert is thrown this is this class which holds all the error logs that are produced in the session. <ul class="logContainer t-logs-A">

Once I have managed to simulate an alert by clearing all data from a particular field, the alert is thrown in this class

<ul class="logContainer t-logs-A">
<li class="logMessage error" id="l1392048208775" style="display: none;">
<header>
<p>error!</p>
<p class="closeBtn"></p>
</header>
<p>An error has occurred : Value must be a Number.</p>
</li>
</ul>

The id is dynamically created so when I attempt to 'copy xpath' I'm supplied with //*[@id="l1392048208775"]/p which is no good.

The alerts that are thrown all remain in the DOM as I can visibly see them, although they are hidden as they fade out from the UI after a couple of seconds.

Any suggestions on how to go about this, please?

share|improve this question
1  
Have you tried changing your CSS selector to .logMessage.error? –  user246 Feb 11 at 0:44
    
Hi @user246 I tried placing a dot before the CSS selector and that did nothing, as I suspected it would. But thanks anyway. –  Django_Tester Feb 12 at 9:00

1 Answer 1

Why not use the selenium Alert switchto method and gettext?

Alert alert = driver.switchTo().alert(); 
String text = alert.getText(); 
alert.dimiss(); 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.