如何检查 Web 元素是否位于左上角



我必须检查徽标是否显示在左上角。我下载了元素的位置,我不知道如何在 JUnit 中编写断言。

Point loc = driver.findElement(By.className("hdr_logo")).getLocation();

位置是 (0,128(

检查 x 和 y 是否小于 200 是个好主意?

您需要获取浏览器窗口的尺寸:

int winHeight = driver.manage().window().getSize().getHeight();
int winWidth = driver.manage().window().getSize().getWidth();

然后你需要获取 Web 元素的位置和大小:

WebElement logo = driver.findElement(By.className("hdr_logo"));
int xPos = logo.getLocation().getX();
int yPos = logo.getLocation().getY();
int eleHeight = logo.getSize().getHeight();
int eleWidth = logo.getSize().getWidth();

为了检查它是否在屏幕的左上象限,最右下角的像素必须在浏览器窗口高度和宽度的一半范围内:

Assert.assertTrue((xPos + eleWidth) <= winWidth/2) && (yPos + eleHeight) <= winHeight/2), "Logo is NOT in the upper left quadrant");

最新更新