“每个步骤脚本工具桌面“中编辑脚本时,可以添加自定义 C# 代码。 请参阅每个步骤脚本 工具对象模型:类、方法、属性等,以查找有关每个步骤脚本 自定义的详细信息。

使用自定义 C# 代码编辑的脚本在保存之前需要获得技术支持团队的批准。 每次添加自定义 C# 代码或编辑包含自定义 C# 代码的脚本时,将自动发送脚本以供审批。 请注意,我们的团队可能需要一些时间来审核和批准脚本以供使用。

下面是将 C# 添加到脚本中以使用条件语句和随机定义的变量的一些示例:

条件语句示例

在内容验证错误上中止脚本执行

Step (1, "test - https://www.test.com");
tab0 = Tabs.NewTab ();
tab0.GoTo ("https://www.test.com");
if ( !tab0.Text.Contains("<KEYWORD>"))
{
return;
}
例如,如果找不到”登录“关键字,则中止脚本,添加:
if (!tab0.Text.Contains("Login"))

关闭条件弹出窗口(如果出现)

脚本重播条件可能因地理区域、时间等而异。 因此,在我们重播脚本时,可以根据条件显示某些内容,例如弹出窗口。 若要指定如何管理目标网站上的条件内容,请将 if 语句添加到“每个步骤”脚本中。

例如,在脚本重播期间关闭页面上显示的弹出窗口并导航到新页面

  1. 记录脚本,并在页面上显示弹出窗口。
  2. 或者:如果您有 Xpath 在目标页面上找到“关闭”按钮,则可以在没有弹出窗口的情况下录制脚本。
  3. 在预期显示弹出窗口的步骤中添加 if 语句。 使用“关闭”按钮 Xpath 指定条件。

请注意,我们在 “if” 测试表达式中使用 WaitForElement 方法来等待弹出窗口加载:

tab0.WaitForElement (TimeSpan timeout, string xpath)
// script_version=3.0; everystep_version=0; date=4/8/2022; Chrome=91.0.4472.77
NetworkFilter.Deny ("https://www.adidas.com/api/chk/customer/baskets?sitePath=us");
Tabs.SetSize (1820, 513);
DMBrowser tab0 = null;
Step (1, "adidas Official Website | adidas US - https://www.adidas.com/us/kids");
tab0 = Tabs.NewTab ();
tab0.GoTo ("https://www.adidas.com/");
tab0.Link ("//A[normalize-space(text())=\"KIDS\"]", "//A[normalize-space()=\"3 STRIPE LIFE\"]/../preceding-sibling::DIV[2]//A[normalize-space()=\"KIDS\"]", "//A[normalize-space()=\"Give Back\"]/../preceding-sibling::DIV[3]//A[normalize-space()=\"KIDS\"]").Click ();
Step (2, "Kids Shoes and Clothing | adidas US - https://www.adidas.com/us/kids");
//Waits for pop-up window with the "Close" button to appear on the page for 5 seconds. 
if (tab0.WaitForElement("5sec".ToDuration (), "//*[@ID=\"modal-root\"]/DIV/DIV/BUTTON", "//BUTTON[@ARIA-LABEL=\"Close\"]"))
{
//If the "Close" button detected, clicks the button to close the pop-up. Otherwise, jumps to the next line.
 tab0.Button ("//*[@ID=\"modal-root\"]/DIV/DIV/BUTTON", "//BUTTON[@ARIA-LABEL=\"Close\"]").Click ();
}
tab0.Span ("//A[normalize-space(@TITLE)=\"SHOP NOW\"]//SPAN[normalize-space()=\"SHOP NOW\"]", "//A[normalize-space(@TITLE)=\"SHOP NOW\"]//SPAN[normalize-space(text())=\"SHOP NOW\"]", "(//SPAN[normalize-space(text())=\"SHOP NOW\"])[1]").Click ();
Step (3, "Kids - Ultraboost 22 | adidas US - https://www.adidas.com/us/kids-ultraboost_22");

将商品添加到购物车或候补名单(如果商品不可用)

如果。。。else 语句让您的脚本执行特定操作,如按钮单击、Chekbox 检查、导航到另一个 URL 等。基于指定的条件。 例如,您可以记录“添加到购物车”交易,并希望脚本将商品添加到购物车,或者,如果商品缺货,则添加到等待列表。 您可以通过添加 if…否则声明:

  1. 记录购物车交易。
  2. 要指定测试表达式,请选择声明商品有库存的元素,例如“立即购买”按钮。
  3. 使用 tab0 导航到项目页面后指定“if”语句。WaitForElement (TimeSpan timeout, string xpath) 方法与 “Buy Now” 按钮 Xpath.
  4. 指定在该项可用作“if”语句时要执行的操作。 例如,“立即购买”点击。
  5. 指定在商品缺货(未检测到“立即购买”按钮)时要执行的操作作为“else”语句。 例如,“添加到列表”单击。

技巧

要指定备用流操作,请在同一脚本中记录除默认流之外的操作。 然后在“else”语句中使用相关行。

检查下面提供的示例。

// script_version=3.0; everystep_version=0; date=4/8/2022; Chrome=91.0.4472.77
Tabs.SetSize (1820, 479);
DMBrowser tab0 = null;
Step (1, "International Shopping: Shop Computers that Ship Internationally - https://www.amazon.com/b?node=16225007011");
tab0 = Tabs.NewTab ();
tab0.GoTo ("https://www.amazon.com/b?node=16225007011");
tab0.Image ("//IMG[normalize-space(@ALT)=\"Seagate Portable 2TB External Hard Drive Portable HDD – USB 3.0 for PC, Mac, PlayStation, &amp; Xbox - 1-Year Rescue Service (...\"]", "/HTML/BODY/DIV[1]/DIV[2]/DIV[2]/DIV[1]/DIV[2]/DIV/DIV/DIV[2]/DIV[3]/DIV/DIV/SPAN/A/DIV/IMG").Click ();
Step (2, "Amazon.com: Seagate Portable 2TB External Hard Drive Portable HDD – USB 3.0 for PC, Mac, PlayStation, & Xbox - 1-Year Rescue Service (STGX2000400) : Electronics - https://www.amazon.com/gp/product/handle-buy-box/ref=dp_start-bbf_1_glance");
tab0.Navigating ("https://www.amazon.com/gp/product/handle-buy-box/ref=dp_start-bbf_1_glance");
tab0.Para ("//P[normalize-space()=\"500 GB\"]", "//P[normalize-space(text())=\"500 GB\"]", "//LI[normalize-space(@TITLE)=\"Click to select 500 GB\"]/SPAN/DIV/SPAN/SPAN/SPAN/BUTTON/DIV/DIV[1]/P").Click ();

// Waits for the "Buy Now" button to be loaded on the page for 5 seconds. 
if (tab0.WaitForElement("5sec".ToDuration(), "//INPUT[@ID=\"buy-now-button\"]", "(//SPAN[normalize-space()=\"Buy Now\"])[last()]/..//INPUT[@TYPE=\"submit\"]", "(//SPAN[normalize-space()=\"Buy Now\"])[last()]/..//INPUT"))
{
If the "Buy Now" button detected, clicks the button. Otherwise, the "Buy Now" click is skipped from execution and the "Add to List" click is executed. 
tab0.Button ("//INPUT[@ID=\"buy-now-button\"]", "(//SPAN[normalize-space()=\"Buy Now\"])[last()]/..//INPUT[@TYPE=\"submit\"]", "(//SPAN[normalize-space()=\"Buy Now\"])[last()]/..//INPUT").Click ();
}
//if the "Buy Now" button not detected, finds and clicks "Add to List" button instead.
else
{
  tab0.Link ("//A[normalize-space()=\"Add to List\"]", "//A[normalize-space(text())=\"Add to List\"]", "//A[@NAME=\"submit.add-to-registry.wishlist.unrecognized\"]").Click ();
}
Step (3, "Amazon Sign-In - https://www.amazon.com/ap/signin?openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Faw%2Fd%2FB07X3XGG3F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&");
tab0.Navigating ("https://www.amazon.com/ap/signin?openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Faw%2Fd%2FB07X3XGG3F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&");

数据随机化

注册过程的电子邮件随机化

要在注册过程中使用随机生成的电子邮件,您可以使用类似于以下内容的随机生成的值声明一个新变量:

string mail = ("test" + Guid.NewGuid().ToString("N") + "@gmail.com");

使用该变量作为电子邮件字段的 TypeText 方法参数,如下所示:

tab0.TextField ("//INPUT[@TYPE=\"text\"]", "//INPUT[@ID=\"Contact_Email_Addr\"]", "//INPUT).TypeText (mail.ToString ());
// script_version=3.0; everystep_version=4.0.8108.2121; date=4/8/2022; Chrome=91.0.4472.77
Tabs.SetSize (1768, 816);
DMBrowser tab0 = null;
//Declares the mail variable.
string mail = ("test+" + Guid.NewGuid().ToString("N") + "@gmail.com");
Step (1, "Dotcom-Monitor: Website Monitoring and Performance Testing - https://www.dotcom-monitor.com/");
tab0 = Tabs.NewTab ();
tab0.GoTo ("http://dotcom-monitor.com");
Tabs.SetSize (1768, 623);
tab0.Span ("(//SPAN[normalize-space(text())=\"Free Trial\"])[1]", "(//DIV[@ID=\"jivo-iframe-container\"]/preceding-sibling::DIV[3]//SPAN[normalize-space(text())=\"Free Trial\"])[1]", "(//IFRAME[normalize-space(@TITLE)=\"Jivochat\"]/../preceding-sibling::DIV[3]//SPAN[normalize-space(text())=\"Free Trial\"])[1]").Click ();
Step (2, "Free Trial Sign Up - https://userauth.dotcom-monitor.com/Account/FreeTrialSignUp");
tab0.Navigating ("https://userauth.dotcom-monitor.com/Account/FreeTrialSignUp");
tab0.TextField ("//INPUT[@TYPE=\"text\"]", "//INPUT[@ID=\"Contact_Email_Addr\"]", "//INPUT").Click ();
//Sets the email value to the randomly generated value of the mail variable.
tab0.TextField ("//INPUT[@TYPE=\"text\"]", "//INPUT[@ID=\"Contact_Email_Addr\"]", "//INPUT").TypeText (mail.ToString ());
tab0.Button ("//BUTTON[normalize-space()=\"Start Free Trial\"]", "//BUTTON[normalize-space(text())=\"Start Free Trial\"]", "//BUTTON[@TYPE=\"button\"]").Click ();

进入18至12岁之间的年龄。

// script_version=3.0; everystep_version=4.0.5953.25078; date=4/19/2016; IE=11.0.9600.17126
Tabs.ConfigureIEVersion (BrowserMode.IE11, DocumentMode.IE11Emulate);
Tabs.SetSize (1768, 651);
DMBrowser tab0 = null;
Step (1, "The input element - HTML5 tutorial - http://www.html-5-tutorial.com/input-element.php");
tab0 = Tabs.NewTab ();
tab0.GoTo ("http://www.html-5-tutorial.com/input-element.php");

// produces random number in range from 18 to 120
int r = (new Random(DateTime.Now.Second)).Next(18, 121);

tab0.TextField ("//INPUT[@TYPE=\"number\"]", "//INPUT[@NAME=\"age\"]", "//B[normalize-space()=\"Age:\"]/..//INPUT").TypeText (r.ToString());

随机选择男性或女性性别

// script_version=3.0; everystep_version=4.0.5953.25078; date=4/19/2016; IE=11.0.9600.17126

Tabs.ConfigureIEVersion (BrowserMode.IE11, DocumentMode.IE11Emulate);
Tabs.SetSize (1768, 714);
DMBrowser tab0 = null;
Step (1, "visible - http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_radio");
tab0 = Tabs.NewTab ();
tab0.GoTo ("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_radio");
for (int i = 0; i < 5; i++)

// produces random number in range from 0 to 2

int r = (new Random(DateTime.Now.Second)).Next(0, 3);
if (r == 0)
{
tab0.Frame ("//IFRAME[@ID=\"iframeResult\"]", "//IFRAME").RadioButton ("//INPUT[@VALUE=\"female\"]", "//INPUT[@VALUE=\"other\"]/preceding-sibling::INPUT[1]", "//INPUT[@VALUE=\"male\"]/following-sibling::INPUT[1]").Click ();
}
else if (r == 1)
{
tab0.Frame ("//IFRAME[@ID=\"iframeResult\"]", "//IFRAME").RadioButton ("//INPUT[@VALUE=\"other\"]", "//INPUT[@VALUE=\"female\"]/following-sibling::INPUT[1]", "//INPUT[@VALUE=\"male\"]/following-sibling::INPUT[2]").Click ();
}
else if (r == 2)
{
tab0.Frame ("//IFRAME[@ID=\"iframeResult\"]", "//IFRAME").RadioButton ("//INPUT[@VALUE=\"male\"]", "(//INPUT[@TYPE=\"radio\"])[1]", "(//INPUT[@NAME=\"gender\"])[1]").Click ();
}
Delay("3sec".ToDuration());
}

从在线产品目录中随机挑选项目

若要创建从联机目录中重复选取随机项的脚本,请使用下面提供的示例。 请注意,建议先记录多个项目单击,以便以后更轻松地编辑原点。

// script_version=3.0; everystep_version=4.0.8108.2121; date=4/12/2022; Chrome=91.0.4472.77
Tabs.SetSize (1768, 714);
DMBrowser tab0 = null;
Step (1, "Amazon.com: Jewelry Made in Italy: Clothing, Shoes & Jewelry - https://www.amazon.com/b?node=19219863011&pf_rd_r=ZS16WVQN9RD996FPB6TH&pf_rd_p=0b9330ec-285a-4f51-9e1c-6765ca451c5f&pd_rd_r=3b9e3cf5-d247-4f50-8d15-a6683fa46846&pd_rd_w=uNsg8&pd_rd_wg=xFg13&ref_=pd_gw_unk");
tab0 = Tabs.NewTab ();
tab0.GoTo ("https://www.amazon.com/b?node=19219863011&pf_rd_r=ZS16WVQN9RD996FPB6TH&pf_rd_p=0b9330ec-285a-4f51-9e1c-6765ca451c5f&pd_rd_r=3b9e3cf5-d247-4f50-8d15-a6683fa46846&pd_rd_w=uNsg8&pd_rd_wg=xFg13&ref_=pd_gw_unk");

//Collects all images to count the total number of images in the body and saves them to the body's Attribute
tab0.RunScript("(function(){const imgCount=document.querySelectorAll(\".s-image\").length;document.body.setAttribute(\"data-imgCount\", imgCount);})()");
int count = 0;

//Gets the value of the Attribute 
string countString = tab0.Element("//BODY").GetAttributeValue("data-imgCount");

//If countString parsed successfully then clicks a random image on the page. Otherwise, raises an error. 
if (!int.TryParse(countString, out count)) {
  RaiseError(string.Format("Failed to get elements count: {0}", countString));
} else {
  var rnd = new Random();
  int i = rnd.Next(1, count);

//Clicks the picked image  
  tab0.Image (string.Format ("(//IMG[@CLASS=\"s-image\"])[{0}]", i)).Click ();

//Navigates to the item page
Step (2, string.Format ("Amazon.com: Selected Item {0}", i));

//Waits for the navigation to be finished. The Navigating (string url) argument can be set to a random URL since it is being scripted while the source script recording and not interfered with the script execution logic. However, the specified URL will be presented in the report data. Check report data for network requests to find an actual URL that was used in the related script run.
tab0.Navigating ("https://www.amazon.com/KEZEF-Creations-Sterling-Overlay-Necklace/dp/B008Y357US/ref=lp_19219863011_1_1");

//Navigates back to the main page and repeats the cycle. You can populate the following lines as many times as necessary within your script.
Step (3, "Amazon.com: Jewelry Made in Italy: Clothing, Shoes & Jewelry - https://www.amazon.com/b?node=19219863011&pf_rd_r=ZS16WVQN9RD996FPB6TH&pf_rd_p=0b9330ec-285a-4f51-9e1c-6765ca451c5f&pd_rd_r=3b9e3cf5-d247-4f50-8d15-a6683fa46846&pd_rd_w=uNsg8&pd_rd_wg=xFg13&ref_=pd_gw_unk");
tab0.Back ();
  i = rnd.Next(1, count);
  tab0.Image (string.Format ("(//IMG[@CLASS=\"s-image\"])[{0}]", i)).Click ();
Step (4, string.Format ("Amazon.com: Selected Item {0}", i));
tab0.Navigating ("https://www.amazon.com/Saint-MIchael-Archangel-Medal-Patron/dp/B01G3K6M5C/ref=lp_19219863011_1_3");
Step (5, "Amazon.com: Jewelry Made in Italy: Clothing, Shoes & Jewelry - https://www.amazon.com/b?node=19219863011&pf_rd_r=ZS16WVQN9RD996FPB6TH&pf_rd_p=0b9330ec-285a-4f51-9e1c-6765ca451c5f&pd_rd_r=3b9e3cf5-d247-4f50-8d15-a6683fa46846&pd_rd_w=uNsg8&pd_rd_wg=xFg13&ref_=pd_gw_unk");
tab0.Back ();
}