Java File.separator example.

Different operating systems use different characters as file and path separators. When our application has to run on multiple platforms, we need to handle these correctly.
To handle this requirement we can use Java provide File.separator.

This is an example of used / at start and the end .

String destination = System.getProperty(“user.dir”) + “/ExtentReport/” + “/Screenshots/” + result.getMethod().getMethodName() + dateName + “.png”;

So instead of using / sign for path separators we could use File.separator

import;String destination = System.getProperty(“user.dir”) + File.separator + “ExtentReport” + File.separator +”Screenshots” + File.separator + result.getMethod().getMethodName() + dateName + “.png”;
To use it you will have to add this import
Example in code

Another example is instead of

File jsonBody = new File(System.getProperty("user.dir") + "//testdata_files//TC-02//" + "CreateIncident.json");

I used

File jsonBody = new File(System.getProperty("user.dir") +File.separator + "testdata_files"+File.separator +"TC-02" +File.separator + "CreateIncident.json");



How to change Java JDK Version of IntelliJ IDEA.

Open IntelliJ IDEA. Then from top menu click File>Project Structure

Then you can change the values.

To see the changes are applied, you can create a class file in the project and run below code.

public class VersionChecker{

public static void main(String[] args) {
Runtime.Version myversion =Runtime.version();
System.out.println("My Java version is--"+myversion);

The output is

My Java version is — 18.0.2+9–61



Webdriver how to verify element is not displayed in Java.

This code snippet can be used when there is a hard coded element.

// WebElement first search result link can be captured using search_results_scroll.
boolean isDiplayedKnowledgeBase=false;
WebElement firstSearchResult = search_results_scroll.findElement(By.cssSelector("ul > li"));
catch(NoSuchElementException e){
System.out.println("Unwanted element is not displayed.");
Assert.assertEquals(false, isDiplayedKnowledgeBase,"This facility is listed for unwanted users.");

If the element is passed form page object model can use below code snippet.

Pass the element from the page Object

elementNotPresentVerification(gatewayLandingPage.lbl_WhatNeedTohappenWithThis);public void elementNotPresentVerification(WebElement element) {
try {
Assert.assertEquals(false, element.isDisplayed(), "Unwanted element is displayed " + element);
} catch (NoSuchElementException e) {
System.out.println("Unwanted element is not found");



Install open JDK 18 and Maven in Windows 10.

Just download open JDK, latest version.

Unzip it and create a folder called Java in C:\Program Files, and move the extracted files in to ,C:\Program Files\Java

Then set system variables as below.

C:\Program Files\Java\jdk-18.0.2\bin

C:\Program Files\Java\jdk-18.0.2

C:\Program Files\Java\jdk-18.0.2\bin

Below commands should work.

java -version

javac -version


Then download maven, binaries from
Extract the file and create folder without spaces in C: drive. Example, C:\Maven and move the extracted files.

Go to system variables and add

After that in path variable , create new and add this .

Open a command line and type
mvn -version



GIT clone a repository based on commit.

Assume that one of the files you needed were deleted accidently and already committed to the current branch and on top of that you had done several commits. However, still you need to get that deleted file.

First identify the commit that just prior to the deletion of the file.

Easy way is identify via GITLAB.

So it’s 91ae3305c61ce8803148dfcdc788ec5d2a6c7ecc

Then to a new location, clone the particular branch using GIT CLI.

git clone -b MybaranchName --single-branch

Then type below command to get the branch in the commit.

git checkout 91ae3305c61ce8803148dfcdc788ec5d2a6c7ecc

Using this can get the branch in that commit.



How to pass WebElement type to JavascriptExecutor

The code is given below.

// Define the JavascriptExecutor.JavascriptExecutor js = (JavascriptExecutor) driver;//Capture the element using any locator strategy.WebElement element =driver.findElement("btnLogin")); //Perform Click on LOGIN button using JavascriptExecutor

js.executeScript("arguments[0].click();", element);