如何使用 if else 和量角器
How to use if else with protractor
我有很奇怪的场景,我不知道如何处理它。
我是测试新手,我有一个网站可以测试我们检查购物车功能是否正常 属性。
我的问题是我们添加了 x 件产品并进行了库存检查。如果存在库存冲突,那么我们需要在继续之前解决它,否则我们就继续。
我设法创建了一个如下所示的函数:
describe("Details page", function () {
detailsPage = new DetailsPage();
// The details page is accessible by the specified URL
it(`Is defined by the URL: ${userData["url"]}${browser.baseUrl}`,
async function () {
await detailsPage.navigateDesktop();
});
// Details page has a form and it can be filled out with user data
it("Has a form that can receive user data",
async function() {
await detailsPage.fillFormWithUserData();
await utils.click(detailsPage.getForm().buttons.nextStep);
});
if (detailsPage.hasStockConflict()) {
// Details page allows the user to fix conflicts in stocks
it('Enables resolution of stock conflicts', async function () {
// Wait for stock to fully load
await detailsPage.hasStockConflict();
await detailsPage.clickAllRemoveButtons();
await detailsPage.clickAllDecreaseButtons();
});
// Details page allows the user to proceed to the next stage when all conflicts (if any) has been resolved
it('Allows the user to proceed to the next stage of purchasing', async function () {
const nextStepButton = detailsPage.getForm().buttons.nextStep;
await utils.elementToBeClickable(nextStepButton);
await utils.click(nextStepButton);
});
}
});
但是我的功能问题是我需要等到我从服务器收到响应,或者我确实遇到了将由以下因素触发的库存冲突:
hasStockConflict() //checks if there is stockConflict message in DOM
否则我将重定向到新页面。
我的问题是,我怎样才能在功能上进行排序以检查是否存在库存冲突,然后我们解决 if 语句,否则我们无需执行任何操作就可以继续(这将带我进入下一页)?
我设置了 1 分钟的超时。 1 分钟后,它将通过失败的测试。
基本上,如果存在库存冲突,我想解决 if 语句,否则我们基本上跳过它。我可能误解了测试的目的,所以各种知识也将不胜感激!
每个测试都应该测试特定的东西。它们不应包含 if...else
分支。相反,您应该针对每个场景进行测试。每个测试都应该需要满足特定场景的初始化数据。
你有两种不同的方法来解决这个问题:
在您查询的资源中设置数据,并为正在测试的场景请求特定数据。
模拟资源,以便请求 return 为正在测试的场景策划的模拟数据。
要补充 Code-Apprentice 提到的内容,您可以设置模拟数据以获得您认为合适的响应。您应该模拟不同的响应,并根据响应在一次测试中做一件特定的事情。没有 if else 的东西在步骤中。
对于您的情况,现在,使用您知道有货的商品或添加始终有货的虚拟商品,并将缺货的虚拟商品添加到您的数据库中。为两者以及您认为合适的方式编写单独的测试。
希望对您有所帮助!
每个人都在说,为了避免将来出现陷阱,应该遵循一些最佳实践...
但是,最佳实践#1 是它始终取决于您的公司、您的产品和您的需求。因此,如果您决定需要走这条路,那就走吧
为什么您的方案不起作用
简短回答,您的 it
块是在浏览器启动之前构建的。那时候
你的功能不能 运行,我假设失败或 returns undefined
回答
话说^,你不能跳过it
,把你的逻辑放在里面就好了
it('Enables resolution of stock conflicts', async function () {
if (detailsPage.hasStockConflict()) {
// Wait for stock to fully load
await detailsPage.hasStockConflict();
await detailsPage.clickAllRemoveButtons();
await detailsPage.clickAllDecreaseButtons();
const nextStepButton = detailsPage.getForm().buttons.nextStep;
await utils.elementToBeClickable(nextStepButton);
await utils.click(nextStepButton);
}
});
我有很奇怪的场景,我不知道如何处理它。
我是测试新手,我有一个网站可以测试我们检查购物车功能是否正常 属性。
我的问题是我们添加了 x 件产品并进行了库存检查。如果存在库存冲突,那么我们需要在继续之前解决它,否则我们就继续。
我设法创建了一个如下所示的函数:
describe("Details page", function () {
detailsPage = new DetailsPage();
// The details page is accessible by the specified URL
it(`Is defined by the URL: ${userData["url"]}${browser.baseUrl}`,
async function () {
await detailsPage.navigateDesktop();
});
// Details page has a form and it can be filled out with user data
it("Has a form that can receive user data",
async function() {
await detailsPage.fillFormWithUserData();
await utils.click(detailsPage.getForm().buttons.nextStep);
});
if (detailsPage.hasStockConflict()) {
// Details page allows the user to fix conflicts in stocks
it('Enables resolution of stock conflicts', async function () {
// Wait for stock to fully load
await detailsPage.hasStockConflict();
await detailsPage.clickAllRemoveButtons();
await detailsPage.clickAllDecreaseButtons();
});
// Details page allows the user to proceed to the next stage when all conflicts (if any) has been resolved
it('Allows the user to proceed to the next stage of purchasing', async function () {
const nextStepButton = detailsPage.getForm().buttons.nextStep;
await utils.elementToBeClickable(nextStepButton);
await utils.click(nextStepButton);
});
}
});
但是我的功能问题是我需要等到我从服务器收到响应,或者我确实遇到了将由以下因素触发的库存冲突:
hasStockConflict() //checks if there is stockConflict message in DOM
否则我将重定向到新页面。
我的问题是,我怎样才能在功能上进行排序以检查是否存在库存冲突,然后我们解决 if 语句,否则我们无需执行任何操作就可以继续(这将带我进入下一页)?
我设置了 1 分钟的超时。 1 分钟后,它将通过失败的测试。
基本上,如果存在库存冲突,我想解决 if 语句,否则我们基本上跳过它。我可能误解了测试的目的,所以各种知识也将不胜感激!
每个测试都应该测试特定的东西。它们不应包含 if...else
分支。相反,您应该针对每个场景进行测试。每个测试都应该需要满足特定场景的初始化数据。
你有两种不同的方法来解决这个问题:
在您查询的资源中设置数据,并为正在测试的场景请求特定数据。
模拟资源,以便请求 return 为正在测试的场景策划的模拟数据。
要补充 Code-Apprentice 提到的内容,您可以设置模拟数据以获得您认为合适的响应。您应该模拟不同的响应,并根据响应在一次测试中做一件特定的事情。没有 if else 的东西在步骤中。
对于您的情况,现在,使用您知道有货的商品或添加始终有货的虚拟商品,并将缺货的虚拟商品添加到您的数据库中。为两者以及您认为合适的方式编写单独的测试。
希望对您有所帮助!
每个人都在说,为了避免将来出现陷阱,应该遵循一些最佳实践...
但是,最佳实践#1 是它始终取决于您的公司、您的产品和您的需求。因此,如果您决定需要走这条路,那就走吧
为什么您的方案不起作用
简短回答,您的 it
块是在浏览器启动之前构建的。那时候
你的功能不能 运行,我假设失败或 returns undefined
回答
话说^,你不能跳过it
,把你的逻辑放在里面就好了
it('Enables resolution of stock conflicts', async function () {
if (detailsPage.hasStockConflict()) {
// Wait for stock to fully load
await detailsPage.hasStockConflict();
await detailsPage.clickAllRemoveButtons();
await detailsPage.clickAllDecreaseButtons();
const nextStepButton = detailsPage.getForm().buttons.nextStep;
await utils.elementToBeClickable(nextStepButton);
await utils.click(nextStepButton);
}
});