Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查web元素是否位于左上角_Java_Selenium_Junit_Assert - Fatal编程技术网

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

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

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

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)这听起来像是一个脆弱的测试需求,它会随着设备和显示分辨率的不同而变化。。。