[英]How can I write the following code more elegantly using LINQ query syntax?
[英]Is there a way to write selinium automation code more elegantly?
框架:selinium chrom 驅動程序; 主要使用 xpaths 來自動化 UI
我的代碼目前如下所示:
public void Delete_calendar()
{
Logger.Info("Deleting calendar with name provided");
_login.SelectOLC(ws);
ws.WaitForCheckPoint("Scheduling");
ws.ClickByXPath("//nav/a/span");
ws.waitforStatus("//ia-action-menu-item");
ws.ClickByXPath("(//input[@type='text'])[5]");
ws.ClickByXPath("//text()[contains(.,'Default')]/ancestor::li[1]");
ws.waitforStatus("//button[text()='Assign']");
Thread.Sleep(10000);
ws.ClickByXPath("//nav/a/span");
ws.ClickByXPath("//ia-action-menu-item");
ws.ClickByXPath("(//a[contains(text(),'Calendars')])[2]");
ws.ClickByXPath("//input[@type='text']");
ws.waitforStatus("//span[text()='Sys_demo']");
ws.ClickByXPath("(//button[@type='button'])[2]");
ws.ClickByXPath("//ia-action-menu-item[contains(text(),'Delete')]");
// ws.ClickByXPath("(//button[@type='button'])[30]");
ws.ClickByXPath("/html/body/ia-dialog/ia-dialog-footer/ia-button[2]/button");
Thread.Sleep(10000);
...所有 ws.Clickby 都是 selinium 調用方法,有沒有辦法可以將所有這些 xpath 或代碼放在外部文件中,然后調用該文件,它的執行方式與上面相同。 基本上我希望我的代碼看起來優雅,不那么笨拙且易於閱讀。
歡迎任何建議。
我的想法是
外部文件' test.txt
'
public void Delete_calendar()
{
Logger.Info("Deleting calendar with name provided");
_login.SelectOLC(ws);
RunSeliniumcodefromExternalfile("test.txt")
}
test.txt file contains
[
ws.WaitForCheckPoint("Scheduling");
ws.ClickByXPath("//nav/a/span");
ws.waitforStatus("//ia-action-menu-item");
ws.ClickByXPath("(//input[@type='text'])[5]");
ws.ClickByXPath("//text()[contains(.,'Default')]/ancestor::li[1]");
ws.waitforStatus("//button[text()='Assign']");
Thread.Sleep(10000);
ws.ClickByXPath("//nav/a/span");
ws.ClickByXPath("//ia-action-menu-item");
ws.ClickByXPath("(//a[contains(text(),'Calendars')])[2]");
ws.ClickByXPath("//input[@type='text']");
ws.waitforStatus("//span[text()='Sys_demo']");
ws.ClickByXPath("(//button[@type='button'])[2]");
ws.ClickByXPath("//ia-action-menu-item[contains(text(),'Delete')]");
// ws.ClickByXPath("(//button[@type='button'])[30]");
ws.ClickByXPath("/html/body/ia-dialog/ia-dialog-footer/ia-button[2]/button");
Thread.Sleep(10000);
]
您可以合並頁面 Object Model 模式。 POM 允許您定義 IWebElements 並在 class 級別用它們的定位器標記它們。 使用此技術,無需為每個操作指定定位器即可對 web 元素執行操作。
下面是一個示例,說明如何使用您為 Delete_Calendar 方法提供的代碼實現此目的。
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Support.PageObjects;
namespace POMExample.PageObjects
{
public class POMExample
{
private IWebDriver driver;
private WebDriverWait wait;
public POMExample(IWebDriver driver)
{
this.driver = driver;
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
//This line instantiates the Iwebelements marked with the FindsBy attribute.
PageFactory.InitElements(driver, this);
}
//The FindsBy attribute allows you to tag an IWebelement with a locator.
[FindsBy(How = How.XPath, Using = "//nav/a/span")]
private IWebElement elem_1;
[FindsBy(How = How.XPath, Using = "(//input[@type='text'])[5]")]
private IWebElement elem_2;
public void Delete_calendar()
{
//Instead of: ws.ClickByXPath("//nav/a/span");
elem_1.Click();
//Instead of: ws.ClickByXPath("(//input[@type='text'])[5]");
elem_2.Click();
//and so on...
}
}
}
如果您仍然熱衷於使用外部資源來存儲定位器,我會進一步補充說,通常使用屬性文件來存儲定位器是流行但不一定是最佳實踐,請參閱https://seleniumjava.com/2017/03/19/ do-not-store-element-locators-in-property-files/了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.