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

Mark
  • 323
  • 1
  • 5
  • 11
  • 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`. – Todor Minakov Feb 03 '18 at 05:53
  • I'm more acustomed to the programming point of view, but sure i'll try it. – Mark Feb 03 '18 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](http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#getting-active-library-instance-from-robot-framework), even the example is specifically for SeleniumLibrary. – Todor Minakov Feb 03 '18 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 :) – Todor Minakov Feb 03 '18 at 11:07
  • Possible duplicate of [Pass existing Webdriver object to custom Python library for Robot Framework](https://stackoverflow.com/questions/23703870/pass-existing-webdriver-object-to-custom-python-library-for-robot-framework) – Todor Minakov Feb 03 '18 at 15:06

2 Answers2

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.
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
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}
Mark
  • 323
  • 1
  • 5
  • 11