【问题标题】:How to do an action if the URL contains - TestCafe如果 URL 包含如何执行操作 - TestCafe
【发布时间】:2022-11-10 22:28:22
【问题描述】:
我希望添加一些操作,前提是页面 URL 是给定的。我正在使用 TestCafe Studio。所以我添加了一个带有以下内容的“自定义脚本”块。但这是错误的,它包含不是一个函数。
const getURL = ClientFunction(() => window.location.href.toString());
const myurl = await getURL;
if(myurl.contains('/update')){
await t.click('#updateInfoSubmit');
}
【问题讨论】:
标签:
testing
automated-tests
e2e-testing
testcafe
【解决方案1】:
首先你需要ClientFunction 模块来使用ClientFunction 方法。那么你的测试应该或多或少像这样 /.还记得在获取 url 时使用 await
import { Selector, t, ClientFunction } from 'testcafe';
fixture `RandomPage`
.page("https://stackoverflow.com/questions/73225773/how-to-do-an-action-if-the-url-contains-testcafe")
// Function that Returns the URL of the current web page
const getPageUrl = ClientFunction(() => window.location.href);
test('Test if url has parmeter', async t => {
await t
.click(Selector("a").withExactText("automated-tests"))
// Use await to get page url
var pageUrl = await getPageUrl();
// Check if url has string
if(pageUrl.includes('tagged')){
console.log("url contains string - tagged")
// await t.click(Selector("a"))
}
});