0

So, I have an alert that has multiple span elements with different error messages. I need to test if a specified message is contained in that alert. For example, in the screenshot below, one of the span elements contains the "The email field is required" message. I tried implementing it in Python and then use it as a keyword in RobotFramework, but i've got stuck at finding the current browser instance. The Parser.py file contains:

from selenium.webdriver.common.by import By
from selenium import webdriver
def parse_alert_msg(elements, message):

    alerts = driver.find_elements(By.XPATH, elements)
    for item in alerts:
        if item.text == message:
            return True
    return False 

This is what i have in my robot file:

Test Function
    Open Website
    Login Successfully as       ${UserName}     ${Password}
    ${message}  Parser.Parse Alert Msg  //div[@class='alert-content']/span[@class='alert-description']  You have logged in.
    Run Keyword If  ${message}=${FALSE}  Fail  There is no such span with that text.
    [Teardown]  Close Browser

Alert danger error

5
  • So the question is actually "how to use the RF browser instance in Python methods", as you already have an algorithm for checking the text? BTW, why don't you implementation the check in pure Robotframework, it's pretty much the same flow - the kw to get all elements for the loop is Get Webelements. Commented Feb 3, 2018 at 5:53
  • I'm more acustomed to the programming point of view, but sure i'll try it. Commented Feb 3, 2018 at 10:55
  • Back to your question - if it's just how to get a reference to a running browser - this is in the user guide, even the example is specifically for SeleniumLibrary. Commented Feb 3, 2018 at 11:01
  • Yet I'd recommend to implement keywords in Robotframework syntax, especially for straightforward tasks like this. Otherwise you'll be missing a lot of is benefits - detailed logging, uniform language etc. Python libs/methods do have their usage - when the functionality is not available in RF, or too cumbersome to accomplish - but this case just doesn't fit the description :) Commented Feb 3, 2018 at 11:07
  • Possible duplicate of Pass existing Webdriver object to custom Python library for Robot Framework Commented Feb 3, 2018 at 15:06

2 Answers 2

1

In my view there is no need for the loop, as you can already do the check using xpath text() function:

Check If Alert Contains Errormessage
    [Arguments]  ${alert-message}
    Page Should Contain Element    
    ...    xpath://div[@class='alert-content']/span[@class='alert-description' and text()='${alert-message}']   
    ...    message=The message "${alert-message}" is not found.
Sign up to request clarification or add additional context in comments.

Comments

0

Okay, this is the solution i've come to:

Check If Alert Contains
    [Arguments]  ${alert-path}  ${alert-message}
    @{alert-msg}  Get Web Elements  ${alert-path}
    : FOR  ${element}  IN  @{alert-msg}
    \   ${element_text}  Get Text  ${element}
    \   Run Keyword If   '${element_text}' == '${alert-message}'  Exit For Loop
    Run Keyword Unless  '${element_text}' == '${alert-message}'  Fail  The message "${alert-message}" is not found.

If anybody can see a better approach, feel free to post it :).

EDIT: I feel this is the most improved answer i could come to. No need to loop and/or to implement too complex logic.

Wait Until Alert Contains
    [Arguments]  ${message}
    Wait Until Element Is Visible   //div[@class='alert-content']
    Element Should Contain  //div[@class='alert-content']      ${message}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.