Batman3030
Batman3030

Reputation: 11

How can I assert the value copied to clipboard was correct?

In my steps, I click a button which automatically copies an email address. How can I assert that the value is what I expect it to be? Was trying to figure out a way to paste it in terminal so I could see what it was copying but if there's a more efficient way to do this, would love to know.

I tried importing pyperclip per some other recommendation but it didn't import correctly.

this is the button that copies the value upon click,

@step('I locate the email icon and click')
def step_impl(context):
    window_before = driver.window_handles[0]
    context.current_element = context.wait.until(
        EC.element_to_be_clickable(
            (EMAIL_ICON)
        )
    )
    scroll_to_webelement(context.driver, context.current_element)
    time.sleep(3)
    context.current_element.click()

it triggers your OS's default email to open a second window so this closes it

@step('I switch to the new window and close it')
def step_impl(context):
    context.wait.until(EC.number_of_windows_to_be(2))
    context.driver.switch_to.window(context.driver.window_handles[-1])
    context.driver.close()
    context.driver.switch_to.window(context.driver.window_handles[0])

I expect it to give the email I copied, but every step I try doesn't seem to work.

Upvotes: 1

Views: 3600

Answers (2)

Yosuva Arulanthu
Yosuva Arulanthu

Reputation: 1574

Store your clipboard content into a variable and can assert it as usual. Please try the below code and let me know whether this helps or not.

Python example

import xerox
from selenium import webdriver

driver = webdriver.Chrome('/usr/local/bin/chromedriver')  
driver.implicitly_wait(15)

driver.get("https://clipboardjs.com/")
driver.find_element_by_xpath("//img[@alt='Copy to clipboard']").click() #clip board content copied here
i = xerox.paste() #clip board content stored into variable i
print i
print i == "npm install clipboard --save" #compare the clip board content against the expected value
driver.quit()

Output:

npm install clipboard --save
True 

Upvotes: 1

Sers
Sers

Reputation: 12255

Here's example how you can test. Test HTML:

<!DOCTYPE html>
<html>
<body>
<input type="text" value="[email protected]" id="mm">
<button onclick="myFunction()">Copy text</button>
<script>
    function myFunction() {
        var copyText = document.getElementById("mm");
        copyText.select();
        copyText.setSelectionRange(0, 99999)
        document.execCommand("copy");
        window.open();
    }
</script>
</body>
</html>

Test Code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
driver.get("file:///Users/***/Desktop/test.html")

# store input value
email = wait.until(ec.visibility_of_element_located((By.TAG_NAME, "input"))).get_attribute("value")
# click on button, that will copy value and open new tab
driver.find_element_by_tag_name("button").click()

# wait for the second window and switch to
wait.until(ec.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[-1])

# open google.com to check copied text
driver.get("https://www.google.com/")

google_q = driver.find_element_by_name("q")
# paste text to the google search input, SHIFT and INSERT keys for MacOS
google_q.send_keys(Keys.SHIFT, Keys.INSERT)
# assert copied value with stored
assert google_q.get_attribute("value") == email
# close current window and switch back to the first one
driver.close()
driver.switch_to.window(driver.window_handles[0])

Upvotes: 0

Related Questions