Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

Is there a way to capture browser logs while running automated test cases with Selenium? I found an article on how to capture JavaScript errors in Selenium

But that is just for Firefox and only for errors. I would like to get all the console logs.

1 Answer

0 votes
by (62.9k points)

Logs from webdriver:

We have a couple of options here.

1. Selenium’s get_log() API:

Selenium offers an API which we can use to demand logs by making a remote call to the selenium web server, for example in Python as follows:

driver_logs = driver.get_log(“driver”)

2. The ‘webdriver.chrome.logfile’ environment property:

We can also set a command-line option while launching the web driver server that will instruct the webdriver process to dump logs to a particular location on the disk. The above webdriver server launch command can be augmented like this:

 java

-Dselenium.LOGGER.level=INFO

-log Path/to/webdriver-server.log

-Dwebdriver.chrome.logfile=/Path/to/chromedriver.log

-jar selenium-standalone.jar

To compare the above two approaches, both can be pretty useful depending on how do we want to use the captured logs. Approach (1) is better when you want to log to a new file on every test because it gives you full control over how you write the captured logs. You can also filter the logs before writing them. The one drawback that I feel with this is that it will incur an additional network call on the web driver server which can be time-consuming if the logs are huge and the server is remote.

Selenium WebDriver allows resizing and maximizing window natively from its API. We use 'Dimension' class to resize the window.

Let’s take the help of Selenium WebDrivers Dimension Class and declare object say 'd' by initializing it with width and height as 420X600 as shown below:

Remember, We need to import the statement 'import org.openqa.selenium.Dimension'

import org.openqa.selenium.Dimension;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

public class browser {

@Test

public void openBrowserwithGivenDimension()

{

WebDriver driver = new FirefoxDriver();

driver.navigate().to("http://google.co.in");

System.out.println(driver.manage().window().getSize());

Dimension d = new Dimension(420,600);

//Resize the current window to the given dimension

driver.manage().window().setSize(d);

}

}

We can set the size and perform testing with lower resolution. We can also perform testing on Responsive sites which automatically get adjust based on the browser size.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training course!

Browse Categories

...