我正在使用 selenium 运行我创建的测试。我现在想开始错误处理。我希望能够获取无法找到的元素并"Element start could not be found"
在我的错误日志中说出类似的话。如果这不能完成,我想打印发生异常的整行代码。
我不知道该怎么做。目前我只是将每个测试包装在一个 try catch 中,并说Couldn't find element
这非常广泛,并没有缩小它找不到的元素。
我的会话设置
private string excelId = CommonMethods.ExcelOfficeVersion();
private const string AppDriverUrl = "http://127.0.0.1:4723";
private static WindowsDriver<WindowsElement> excelSession;
System.Web.Caching.Cache cache = new System.Web.Caching.Cache();
xl.Workbook WB;
public static bool skipTearDown = false;
WindowsElement create;
WindowsElement blankWorkBook;
public static DesiredCapabilities appCapabilities = new DesiredCapabilities();
[TestInitialize]
public void SetUp()
{
try
{
appCapabilities.SetCapability("app", excelId);
var initialSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);
var capabilities = new DesiredCapabilities();
capabilities.SetCapability("app", "Root");
excelSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), capabilities);
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
CommonMethods.keyCheck(excelSession);
//blankWorkBook = excelSession.FindElementByName("Blank workbook");
//cache.Insert("Create", create);
CommonMethods.checkCreateExcel();
}
catch (Exception)
{
CommonMethods.ExceptionHandler("WinApp Driver failed to load Excel", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
}
这是我的代码示例
try
{
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("Blank workbook").Click();
excelSession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
excelSession.FindElementByName("Create").Click();
skipTearDown = true;
}
catch (Exception)
{
CommonMethods.ExceptionHandler("Couldn't find element", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
另一个典型的测试
public void newExcelWorkbook()
{
try
{
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("Blank workbook").Click();
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("Create").Click();
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("New").Click();
CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the New button");
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
excelSession.FindElementByName("E Sample Data").Click();
CommonMethods.IsElementDisplayed(excelSession, new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon", "Error appeard while selecting the E Sample Data button");
Thread.Sleep(4000);
excelSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
}
catch (Exception)
{
CommonMethods.ExceptionHandler("Couldn't find element", new StackTrace(true).GetFrame(0).GetFileLineNumber(), new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
TearDown();
}