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 java.io.File;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;
WebCommand.pause(6000);
try{
WebElement firstSearchResult = search_results_scroll.findElement(By.cssSelector("ul > li"));
isDiplayedKnowledgeBase=firstSearchResult.isDisplayed();
}
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.
https://jdk.java.net/18/

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.

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

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

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

Below commands should work.

java -version

javac -version

jshell

Then download maven, binaries apache-maven-3.8.6-bin.zip from https://maven.apache.org/download.cgi
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
MAVEN_HOME
C:\Maven\apache-maven-3.8.6

After that in path variable , create new and add this .
%MAVEN_HOME%\bin

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 https://github.com/Org/Project.git

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(By.name("btnLogin")); //Perform Click on LOGIN button using JavascriptExecutor

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

--

--

How to capture a tag in same level using Sibling “+” Operator CSS Selector in Selenium.

Problem scenario

I want to capture this input tag, located in div tag, but in the same level of span.

How to do it ?

First of all capture the div that span tag is under for that use below CSS locator.

WebElement divTag=divdiv.findElement(By.cssSelector("div[class='search-box']"));

Then using divTag element can capture the input that in the same level.

Syntax for it is +.

locator 1+ locator 2

span[class='icon-filter'] + input

So here to for locator one I captured using class name and locator 2 is using input tag.

Full code is as per below.

WebElement div_Second=divdiv.findElement(By.cssSelector("div:nth-of-type(2)"));// get the second div.
WebElement icon_filter=div_Second.findElement(By.cssSelector("span[class='icon-filter'] + input"));
icon_filter.sendKeys("CORE");

--

--