0

I'm trying to build a framework for test automation based on pytest_bdd. I'm able to perform many functionalities on my Social Networking Site where I'm performing my automation.

So, my use case is, after logging in, I need to search for a user and click on 'Add Friend' button. If manually I try to give specific xpath, I'm able to do that. But when I'm searching for multiple users having same name, say for example 'Nitin kumar', there are 2 users with same name. I want to add both of them as my friend, but I'm not able to click on them.

My steps are the following:

  • Logging in
  • Search for a user(Nitin kumar)
  • Click on 'Add Friend' button - This is where I'm having problem

Necessary snippet from test_nitsanon.py

@when('the user clicks on add as friend besides a user')
def sendFrndReq(browser):
    res, xp = clickBtn_multiple(browser, sendReq_addFrndBtn)
    print('\n\n\n Sent Friend Request to all users with mentioned name >>> DEBUG\n', res, xp)

Snippet from functions.py

# Clicks a button on every iteration
def clickBtn_multiple(browser, xpath):
    wait(3)
    res = []
    xp = []
    s = browser.find_elements(By.XPATH, xpath)
    for i in s:
        res.append(i.text)
        if xpath.is_displayed():
            a = clickBy_Xpath(browser, xpath)
            xp.append(a)
        else:
            continue
    return res, xp

Snippet from xpaths.py

# Send friend request
sendReq_addFrndBtn = "//div[text()='" + sendReq_name + "']/ancestor::div/div/div/div/div/div[2]/child::div[2]/div/form/div/button"

Snippet from variables.py

nitsanonURL = "http://nitsanon.epizy.com/"

userValue = 'nitin'
passValue = 'kumar'
sendReq_name = "Nitin kumar"

What is wrong with this code? I guess there is something wrong with the function I've made! The return statement is returning an empty list. Check the output image here.

1 Answers1

0

I've enhanced my code for the functions.py file. But then also I'm getting StaleElementReferenceException for checking out different functionality.

Following is the updated code:

def clickBtn_multiple(browser, xpath):
    wait(3)
    res = []
    xp = []
    s = browser.find_elements(By.XPATH, xpath)
    print(s)

    for i in s:
        if i.is_displayed():  # Changed xpath.is_displayed() to i.is_displayed()
            res.append(i.text)
            a = clickBy_Xpath(browser, xpath)
            xp.append(a)
    return res, xp

I'm now trying to achieve the 'Unfriend' functionality. To navigate to the FRIENDS tab & Unfriend users with a specific name.

Code snippet for test_nitsanon.py

@when('the user clicks on unfriend besides a user')
def sendFrndReq(browser):
    res, xp = clickBtn_multiple(browser, unfrnd_Btn)
    print('\n\n\n Unfriended to all users with mentioned name >>> DEBUG\n', res, xp)

Code snippet for xpaths.py

navOption_friends = "//a[contains(text(),'Friends')]"
unfrnd_Btn = "//div[text()='" + sendReq_name + "']/ancestor::div/div[2]/child::div[1]/a"

The output I'm getting along with the console exception is here.

This is happening because after clicking on the 'UnFriend' button one time, the page refreshes and navigates to a different page.

I have a logic to check everytime with a value 1(let's say) during 1st iteration & will save the value till the next iteration. Till then, we'll navigate back to the FRIENDS tab & again go back to this function for the remaining iterations. But, I'm not able to implement this one.

Need answers. Not able to sleep. I'm so much curious to build a framework for test automation without using any OOPs concept.