1

Background : Web page get blank (So script fails as there is no element to locate, only blank page) during selenium script execution in random place only on staging server while same working on production.

Is there a way in selenium to get the page size ? I want to get the page size and check whether it is blank (means no content rendered ) if so then hit the URL again.

I'm thinking to manage this code in tear down method which logs the test failure :

@AfterMethod
public void tearDown(ITestResult result)
{
    if(ITestResult.FAILURE==result.getStatus())
    {
        try 
        {
            TakeScreenshot.failedScreenShot(result.getMethod().getMethodName());

            LogWriter.logger.info(result.getThrowable().toString());
        } 
        catch (Exception e)
        {
            LogWriter.logger.info("Exception while taking screenshot "+e.getMessage());
        } 
    }
} 

Can someone please suggest me if it is possible or is there any alternative that i can implement the same for my deed.

2

The other answer from @Kenil Fadia details out how to get the screen dimensions, I don't think this will help. The browser viewport, whether it is rendering anything or not will always have a minimum value.

In other words, a normal webpage, a webpage with nothing in the body, and a completely blank file will all have the same dimensions. Extra long pages will have a larger value, but none of them with have 0 or any other good trigger.

You will probably be better off recording the source code and go from there:

  • If there is no content, you know something is broken before the page is even rendered
  • If there is source, see if it stops at some point (breaks part way through the render)
  • If the source is complete, see if all the external links are correct (maybe the staging server is pointing to the production server and is being blocked)
0

You can use javascriptExecutor and use the window.innerWidth and window.innerHeight property.

System.setProperty("webdriver.gecko.driver", "<path to gecko driver>")
WebDriver driver = new ChromeDriver();
driver.get("<someurl>");
JavascriptExecutor js = (JavascriptExecutor) driver;
int contentHeight = ((Number) js.executeScript("return window.innerHeight")).intValue();
int contentWidth = ((Number) js.executeScript("return window.innerWidth")).intValue();

Reference link - https://www.w3schools.com/jsref/prop_win_innerheight.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.