How to take Screenshots if Test Case fails?


One basic question which comes in everyone's mind is : What happens if test case fails? Where was the error in script and How can we capture it?

So, the solutions is always (at least in most of the cases) to take screenshots of webpage when the test run fails.

With one look at the screenshot we can get an idea of where exactly the script got failed. Moreover reading screenshot is easier compare to reading 100's of console errors

To get screenshot on test failure, we should put the entire code in try-catch block. In the catch block we should paste the screenshot code:

public class TakeScreenshot {
 
   WebDriver driver;
 
 @BeforeTest
 public void start(){
  driver = new FirefoxDriver();
 }


Run a Bat File with Java Program


Sample JAVA Code:

public class RunBat {

public static void main(String[] args) throws IOException, InterruptedException{

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c F:\\bb.bat");
process.waitFor();
}
}

How to handle Authentication PopUp using Selenium?

Sometime we have to automate websites which shows authentication Pop Up like mentioned below:











In order to automate this PopUp, we will pass UserId and Password via URL:

http://username:password@anytestwebsite.com

Java Script:

String URL = "http://" + username + ":" + password + "@" + anytestwebsite.com;
driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();