为了账号安全,请及时绑定邮箱和手机立即绑定

如何断言复制到剪贴板的值是正确的?

如何断言复制到剪贴板的值是正确的?

ITMISS 2021-11-12 15:02:14
在我的步骤中,我单击了一个自动复制电子邮件地址的按钮。我如何断言该值是我期望的值?试图找出一种将其粘贴到终端中的方法,以便我可以看到它在复制什么,但是如果有更有效的方法来做到这一点,我很想知道。我尝试根据其他一些建议导入 pyperclip,但没有正确导入。这是点击时复制值的按钮,@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()它会触发您操作系统的默认电子邮件以打开第二个窗口,以便将其关闭@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])我希望它给我复制的电子邮件,但我尝试的每一步似乎都不起作用。
查看完整描述

2 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

下面是如何测试的示例。测试 HTML:


<!DOCTYPE html>

<html>

<body>

<input type="text" value="my@email.com" 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>

测试代码:


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])


查看完整回答
反对 回复 2021-11-12
?
精慕HU

TA贡献1845条经验 获得超8个赞

将剪贴板内容存储到一个变量中,并可以像往常一样对其进行断言。请尝试下面的代码,让我知道这是否有帮助。


Python 示例

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()

输出:

npm install clipboard --save

True 


查看完整回答
反对 回复 2021-11-12
  • 2 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信