How to upload a file in remote webdriver.

In this example, let’s look at the possibility of attaching an image to JIRA.

Code is as per below-

import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
String filePath = System.getProperty("user.dir") + "/src/Attachments/SnowAttachment.png";
WebElement element = findElements(By.xpath("//element[@attribute='value']"));
element.sendKeys(inputText);

--

--

Import the parent project to the IntelliJ IDEA as a new module.

First import the child project, then click , File > New > Project from existing source

and select the parent project.

Click Next, by selecting create project from existing sources.

--

--

How to close the second tab once it’s functionality is done , in Webdriver Java.

The code is

public static void closeSecondTabAndSwitchToFirstTab(WebDriver driver){
ArrayList<String> tab = new ArrayList<String> (driver.getWindowHandles());
System.out.println("No of tabs are --"+tab);
if (tab.size() == 1) {
System.out.println("No additional tabs.");
driver.switchTo().window(tab.get(0));
} else {
//Close the second window.
driver.switchTo().window(tab.get(1));
driver.close();
System.out.println("Closed the second tab using index 1.");
driver.switchTo().window(tab.get(0));
System.out.println("Switched to main window using index 0.");
}
}

--

--

IntelliJ IDEA takes ages to update the indices.

When I opened my IntelliJ IDEA, it says updating indexes but even after an hour , it’s still updating how to solve this .

Open IntelliJ IDEA, save all your code.

Select options as below and click Invalidate and Restart

--

--

How to access Test plan’s keys (variables) using ISuite suite.

Recently, I had a requirement to get a value from Test plan xml file to a onStart method in SuiteListener class which implements ISuiteListener

public class SuiteListener implements ISuiteListener {
@Override//This is equal to before Suite.
public void onStart(ISuite suite) {
}
}

To solve that I used suite.getAttribute method.

public void onStart(ISuite suite) {
String value = (String) suite.getAttribute("browserVersion");
String browserVersion= suite.getXmlSuite().getParameter("browserVersion");
System.out.println("browserVersion is "+browserVersion);
}

So it’s output the below output.

browserVersion is 95

My Test plan is like below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Problem-Management-Suite" parallel="tests">
<listeners>
<listener class-name="com.sam.reporting.SuiteListener" />
<listener class-name="com.sam.reporting.Listeners" />
</listeners>
<parameter name="browser" value="Chrome" />
<parameter name="browserVersion" value="95" />
<test name="test name-01">
<classes>
<class name="com.sam.testcases..TC_01"/>
</classes>
</test> <!-- Test -->

</suite>

--

--

Get current date time in a defined format with IST time zone

For that could use below code in Java 1,8 or above.

public static  String  getCurrentDate(){
final ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
String Temporal= DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now);
String today= LocalDate.now().format(DateTimeFormatter.ofPattern(Temporal));
return today;
}

pattern can be further modified. Here it’s return time as well.

String Temporal= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now);

Output is

2021–11–05 17:51:50

A list of time zones could be retrieved from https://mkyong.com/java/java-display-list-of-timezone-with-gmt/

--

--