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

在我的步骤中,我单击了一个自动复制电子邮件地址的按钮。我如何断言该值是我期望的值?试图找出一种将其粘贴到终端中的方法,以便我可以看到它在复制什么,但是如果有更有效的方法来做到这一点,我很想知道。


我尝试根据其他一些建议导入 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])

我希望它给我复制的电子邮件,但我尝试的每一步似乎都不起作用。


ITMISS
浏览 156回答 2
2回答

慕尼黑5688855

下面是如何测试的示例。测试 HTML:<!DOCTYPE html><html><body><input type="text" value="my@email.com" id="mm"><button onclick="myFunction()">Copy text</button><script>&nbsp; &nbsp; function myFunction() {&nbsp; &nbsp; &nbsp; &nbsp; var copyText = document.getElementById("mm");&nbsp; &nbsp; &nbsp; &nbsp; copyText.select();&nbsp; &nbsp; &nbsp; &nbsp; copyText.setSelectionRange(0, 99999)&nbsp; &nbsp; &nbsp; &nbsp; document.execCommand("copy");&nbsp; &nbsp; &nbsp; &nbsp; window.open();&nbsp; &nbsp; }</script></body></html>测试代码:from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ecdriver = webdriver.Chrome()wait = WebDriverWait(driver, 5)driver.get("file:///Users/***/Desktop/test.html")# store input valueemail = wait.until(ec.visibility_of_element_located((By.TAG_NAME, "input"))).get_attribute("value")# click on button, that will copy value and open new tabdriver.find_element_by_tag_name("button").click()# wait for the second window and switch towait.until(ec.number_of_windows_to_be(2))driver.switch_to.window(driver.window_handles[-1])# open google.com to check copied textdriver.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 MacOSgoogle_q.send_keys(Keys.SHIFT, Keys.INSERT)# assert copied value with storedassert google_q.get_attribute("value") == email# close current window and switch back to the first onedriver.close()driver.switch_to.window(driver.window_handles[0])

精慕HU

将剪贴板内容存储到一个变量中,并可以像往常一样对其进行断言。请尝试下面的代码,让我知道这是否有帮助。Python 示例import xeroxfrom selenium import webdriverdriver = webdriver.Chrome('/usr/local/bin/chromedriver')&nbsp;&nbsp;driver.implicitly_wait(15)driver.get("https://clipboardjs.com/")driver.find_element_by_xpath("//img[@alt='Copy to clipboard']").click() #clip board content copied herei = xerox.paste() #clip board content stored into variable iprint iprint i == "npm install clipboard --save" #compare the clip board content against the expected valuedriver.quit()输出:npm install clipboard --saveTrue&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript