有没有办法更优雅地编写硒自动化代码?



framework:Selenium chrom driver ;主要使用 xpath 来自动化 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);
]

可以合并页面对象模型模式。POM 允许您定义 IWebElements,并在类级别使用其定位器对其进行标记。使用此技术,可以在不为每个操作指定定位器的情况下对 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/以获取更多信息。

相关内容

  • 没有找到相关文章

最新更新