Java 如何检查web元素是否位于左上角
我必须检查一下标志是否显示在左上角。我下载了元素的位置,不知道如何在JUnit中编写断言Java 如何检查web元素是否位于左上角,java,selenium,junit,assert,Java,Selenium,Junit,Assert,我必须检查一下标志是否显示在左上角。我下载了元素的位置,不知道如何在JUnit中编写断言 Point loc = driver.findElement(By.className("hdr_logo")).getLocation(); 位置是(0128) 检查x和y是否小于200是个好主意?您需要获得浏览器窗口的尺寸: int winHeight = driver.manage().window().getSize().getHeight(); int winWidth = driver.man
Point loc = driver.findElement(By.className("hdr_logo")).getLocation();
位置是(0128)
检查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");
Assert.assertTrue((xPos+eleWidth)这听起来像是一个脆弱的测试需求,它会随着设备和显示分辨率的不同而变化。。。