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.

I am automating a site in which there is a section to create forums. Every time I create a forum it displays message as forums created and hides after 1 section. I have to check the displayed message, But many times it throws exception as "Element is not usable"

@FindBy(id="create_forums")
private WebElementFacade forums;

 @FindBy(id="forum_message")
private WebElementFacade message;
forums.click();
 assertThat(message.getText(),equals("expected"); //here the element message throws "Element is not usable exception" as it hides with in two seconds.. 

public String getMessage() {
    String messagetxt = "";
    try {
        if (Message.isDisplayed()) {
            messagetxt = Message.getText();
        } else {
            messagetext = ((JavascriptExecutor) getDriver()).executeScript(
                    "return arguments[0].innerHTML", Message).toString();
            logger.info("message"+messagetxt);
        }
    } catch(NoSuchElementException ne) {
        logger.info("status message not displayed..... checking from the page source"+ nsee);
    }
    return messagetxt;
}

How can I find the element or at least handle the exception

share|improve this question
    
as per the reply by sam woods i have made some changes in the code and my new code is available above, here I have handled exception. But the else block is not executed, Is there any way to execute the else block once the exception occurs?? –  Ajmal Feb 7 at 10:44
add comment

2 Answers 2

You may or may not be able to interact with the element in time, depending on the speed of your computer and your browser.

There are selectors that are faster or slower. Typically, selecting by ID is the fastest, followed by selecting by Name, and in most browsers, CSS selectors are the next fastest. Since you are already selecting by ID, there's probably not a whole lot you can do to make it find the element faster.

If you are just trying to validate the text of the message, you can use the javascript executor to execute some javascript to get back the text of the element even if it is hidden, but you won't be able to validate that the message was actually displayed - only that the text of the message is what you expect.

Not sure exactly what language you're using, but in Java you could write something like this:

try{
  assertThat(message.isDisplayed())
}
catch(Exception e){
  //Log unable to verify message displayed
}
String messageText = ((JavascriptExecutor) driver).executeScript("return arguments[0].innerHTML", message);
assertThat(messageText,equals("expected");
share|improve this answer
add comment

Here you can find a lot of help. There are lot of people who use selenium/webdriver.

share|improve this answer
    
Why I get -1? I show him/here where he can find help. Did I do something wrong? –  SayusiAndo Feb 6 at 17:40
    
Probably because you didn't answer the question you just linked to a user group with no context. –  Ardesco Feb 7 at 14:39
    
Probably. Tahnks for the answer! :) –  SayusiAndo Feb 7 at 14:49
add comment

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.