Selenium Webdriver:我如何跳过失败的Step&继续下一步

时间:2014-04-18 13:33:35

标签: c# selenium

我的情景如下:

我在上方菜单中有5个按钮/链接。我想通过点击它们来访问每个页面,并希望确定页面内容是否正确加载/显示。所以,为此,我做了以下事情:

第1步:

:Browser:   :Search:    :Compare:   :Contact Us:

第2步:

 a. I click on 'Browse Page'
 b. After load the Browse Page, I Capture/Get the Header Text
 c. Then I Use IF ELSE to check whether Header Text is matched with my text or not
 d. If header text matched, I write "PASS" in Console
 f. If header text didn't match, I write "FAIL" in Console

第3步:

一个。  湾  ...

我的问题:
如果某种方式我的页面没有正确加载或崩溃,我的代码无法通过FindElements(By.XPath)或其他人捕获标题文本。然后我的Selenium Code停止工作。

所以,我希望在这种情况下,如果它找不到Crash的页面标题或其他原因......它将JUMP到NEXT STEP来检查其他页面。

IWebElement Menu_Browse = driver.FindElement(By.XPath("/html/body/form/div[4]/div/div/div[2]/div/div[2]/div/ul/li/a"));
    Menu_Browse.Click();//Goto Browse page

if (driver.FindElement(By.XPath("/html/body/form/div[4]/div/div/div[2]/div/div/h3")).Displayed)
            {
               Console.WriteLine("SUCCESS! - BROWSE Page Appears Properly without any CRASH!");
            }
            else
            {
                Console.WriteLine("FAIL! - BROWSE Page may be CRASH! -- JUMP to Next Step of Testing!!");
            }

2 个答案:

答案 0 :(得分:0)

如果5个元素是独立的(我认为它们是基于你的描述),只需编写5个测试,而不是编写一个大的测试来测试所有内容。这样,您就可以检测到独立的故障,并最大限度地减少代码单元。

如果你真的想跳,你可以: - 使用非失败方法,这里可以考虑使用findElements而不是findElement,然后测试此方法返回的元素数。 - 尝试捕获抛出异常的行并手动处理这种情况(请注意,常见的情况是将错误作为可接受的工作流程的一部分来捕获异常)。

答案 1 :(得分:0)

您可以将Boolean Element值存储在变量中,无论是否显示,然后为该特定值写一个 if condition

If true 
 (ur code)
If false
 (ur code)