【发布时间】:2018-09-18 12:36:38
【问题描述】:
这里是测试场景
- 第 1 步:- 输入用户名和密码
- 第 2 步:- 如果用户已经登录,将出现警报/弹出窗口 带有注销链接,然后单击注销链接并尝试登录 再次。
这是该警报的 HTML 代码
<div class="top-alert hidden-xs hidden-sm">
<div id="alert0" class="alert pull-right alert-danger alert-dismissable" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error:</strong> Invalid Inputs!
</div>
<div id="alert2" class="alert pull-right alert-warning alert-dismissable" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Warning:</strong> Your Login is limited
</div>
<div id="alert3" class="alert pull-right alert-warning alert-dismissable" style="display:none;">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Warning:</strong> You are Already Loggedin <a href="http://localhost/myapp/public/logout">Logout</a>
</div>
</div>
这里是网站screenshot
这是我的解决方案
public class Test
{
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\\\Selenium\\\\drivers\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://localhost/myapp/public/login");
// Maxmimize windows
driver.manage().window().maximize();
// Login the Tripmaker
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("test@test.com");
driver.findElement(By.id("password")).sendKeys("s123456");
driver.findElement(By.cssSelector("#lsbt")).click();
// Explicit wait
if (!driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).isDisplayed())
{
WebDriverWait alert = new WebDriverWait(driver, 5);
alert.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")));
driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).click();
}
else if (driver.findElement(By.xpath("//div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]")).isDisplayed())
{
System.out.println("This step is skipped");
}
}
}
我的代码正在运行,但如果在登录时显示警报,则会引发错误
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')] (tried for 5 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:113)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
at demo.myapp.main(myapp.java:41)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //div[@class='top-alert hidden-xs hidden-sm']/div[@id='alert3']/a[contains(text(), 'Logout')]
我试过.isDisplayed()、.isSelected() 和.isEnabled ()。
【问题讨论】:
-
首先,我强烈建议您看一下 Page-Object-Model。这使得代码更具可读性和可维护性。至于你的问题:只是通过类名猜测:一旦显示警报,具有类“top-alert”的 DIV 是否可能会更改类“hidden-xs”和“hidden-sm”?
-
@JeffC 请不要将
<blockquote>添加到错误跟踪日志中,因为错误跟踪日志会被截断并且几乎不可能调试错误跟踪日志。 -
@Newcontributor 我从未见过有人因为使用 blockquote 而被截断,而且我已经引用过或看到过一些非常非常大的引号。我确认这个也没有被截断。你有一个被截断的例子吗?
-
@JeffC 我之前使用了错误的术语。而不是 truncated 读取为 日志被包装。我坚信现在这个例子几乎是不言自明的。