2 回答

TA貢獻(xiàn)1848條經(jīng)驗 獲得超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])

TA貢獻(xiàn)1845條經(jīng)驗 獲得超8個贊
將剪貼板內(nèi)容存儲到一個變量中,并可以像往常一樣對其進(jìn)行斷言。請嘗試下面的代碼,讓我知道這是否有幫助。
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
添加回答
舉報