1

New to automation, I have a couple of months under my belt and learning when I have free time.

I'm trying to confirm that a user name appears inside a table and the name will appear on several rows.

I'm using something like this:

@step('Users homepage my lists created by is only user "{username}"')

def step_impl(context, username):

    users_name = context.browser.find_elements_by_xpath(
        "//*[@id='apollo-table_wrapper']][contains(text(),'%s')]" % username)

I know the xpath is correct for the table but if I want to verify that only a specific username is visible on the screen I'm running into an issue.

In this image below I want to have a test that makes sure only the user of "mike" is present on the page. I will call out "mike" in the feature file...

General idea of the UI

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • Can you post your full code section and the HTML section to check if XPATH its ok? I dont know what is your contect.browser etc – Wonka Jun 26 '19 at 16:17

2 Answers2

1

You have a typo change ] to //:

users_name = context.browser.find_elements_by_xpath(
        "//*[@id='apollo-table_wrapper']//[contains(text(),'%s')]" % username)

To loop through you can do something like this:

users_names = context.browser.find_elements_by_xpath("//*[@id='apollo-table_wrapper']")
print([i.text for i in users_names if i.text == "Mike"])
# Or you can append to list:
res = []
[res.append(i.text) for i in users_names if i.text == "Mike"]
print(res)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • I think I may need to do a loop in this case.... identify the table then loop through to ensure only the users name appears... – Mike Guzman Jun 26 '19 at 17:07
0

If i understood you, you wan't to verify all the names in the grid, and detect if any of these names isn't "Mike". You can use the code below to select all the names from the grid, and then verify if any of them is different from the expected

#This will only work if the Xpath bellow selects only the names, if it 
#selects other fields from the table it will verify if all the fields are 
#"Mike", instead of only the Name fields.
users_name = context.browser.find_elements_by_xpath("//*[@id='apollo-table_wrapper']")
result = true
expectedValue = "Mike"

for x in users_name:
    if x.text != expectedValue
        result = false
        break
#Assert result here

Let me know if this doesn't work.

Valga
  • 459
  • 2
  • 7
  • Thanks...I will give this a shot...I kind of have something like this in the works...appreciate the help – Mike Guzman Jun 26 '19 at 21:11
  • I tried the second part of this and ran into an issue......Im basically looking to say if you find "Jeff" fail the test and print "Jeff"... What Im getting now is a pass and a print of Jeff...Im expecting a fail and a print of jeff @step('Find logged in user "{logged_in_name}"') def step_impl(context, logged_in_name): find_user = context.browser.find_elements_by_xpath("//a[contains(text(),'%s')]" % logged_in_name) expected_value = "Mike" for x in find_user: if x.text != expected_value: print("Failed\n", expected_value) – Mike Guzman Jun 28 '19 at 19:06