Monday, 23 July 2018

How to Install Java in macOS

The best approach to install Java on the Mac is to get the latest version of Java JRE directly from Oracle. This is fairly straight forward and you can either initiate the process from the terminal application in macOS or by going directly to the Java download page on Oracles website.
Open the Terminal app and type the following:
java
Click on the “More Info” button to jump to the Java downloads page in a web browser
Start installing Java on MacOS
Start installing Java on MacOSThis will allow you to download and install the newest most recent version of Java available for the Mac, which is currently JRE8.

Remove Oracle Java cache on Mac

Below are the steps to clear Java cache from any Mac machine:

  1. Click on the Finder icon located in your dock
  2. Click on Go in the Finder menu
  3. Click on Utilities
  4. Double-click on the Terminal icon
  5. In the Terminal window Copy and Paste the commands below:
    rm -r ~/"Library/Application Support/Oracle/Java"

Uninstall Oracle Java using the terminal from any Mac machine

Note: To uninstall Java, you must have Administrator privileges and execute the remove command either as root or by using the sudo tool.

Remove one directory and one file (a symlink), as follows:
  1. Click on the Finder icon located in your dock
  2. Click on Go in the Finder menu
  3. Click on Utilities
  4. Double-click on the Terminal icon
  5. In the Terminal window Copy and Paste the commands below:
    sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
    sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
    sudo rm -fr ~/Library/Application\ Support/Oracle/Java
Do not attempt to uninstall Java by removing the Java tools from /usr/bin. This directory is part of the system software and any changes will be reset by Apple the next time you perform an update of the OS.

Sunday, 29 October 2017

Uploading file with the help of Robot class in Selenium Automation

While implementing automation, we always come across with a situation where our code need to upload a file using our selenium code.

As we know Selenium is a Web Automation tool and which don't support windows automation. In order to upload a files, we have to perform some Windows based operations.

There are different ways to achieve this. One of the way is to take the support of Robot and StringSelection class.

Below simple code will help to perform file upload as part of Selenium Automation:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUpload {

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

// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://html.com/input-type-file/");

                // Absolute path of the file need to uploaded
String fileLocation = "C:\\Users\\testData.xlsx";

                // the code which helps us to perform Copy and Paste options from Clipboard
StringSelection stringSelection = new StringSelection(fileLocation);

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
                driver.findElement(By.id("fileupload")).click();
// CTRL+V and ENTER keys are the keyboard events to achieve the Keyboard operations
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);

Thread.sleep(5000);
driver.quit();
}
}

Monday, 9 October 2017

Reading a file as String

Sometimes we need to read test input from a file(.txt/.xml etc) and enter into a text area field. Below code will help to read the content of file and stored in a string.

To achieve this we need to take the help of Apache CommonsIO. This will be available at Apache CommonsIO and download  zip file

Extract the zip file and you will find commons-io-2.5.jar file

Add the jar file to the project 

import java.io.File;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;

public class ReadFileToString {
public static void main(String[] args){
File inputFile = new File("d://test.xml");
//location of the tile
try {
               //Reading file to string
String str = FileUtils.readFileToString(inputFile,Charset.forName("utf-8"));
   System.out.println(str);
} catch (Exception e) {
    e.printStackTrace();
}
}
}

With the help of above code, we can have the content of file. Now we can use the variable to enter the text in text area input field.

In this case, 

driver.findElement(By.id("TextArea")).sendKeys(str);

Friday, 3 January 2014

Running Selenium Script from console/command prompt using .class instead of .java file - TestNG

Purpose: Running Selenium Script from console/command prompt using .class instead of .java file with out JDK - TestNG

Some times we need to run our automation script at client box to demonstrate our ability to automate but we don't want to share our code with out JDK ( required - JRE). We can achieve this by following below steps. With this we can run the script from .class instead of .java file.

We can achieve this using both JUnit and TestNG. In this example we are working with TestNG

Here my example is GoogleConsole.java

Step 1: Prepare required scrip using any IDE and compile
Step 2: Open command prompt in another system and check whether JRE is available
Step 3: Prepare testing.xml file as follows

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Google">
  <test name="GoogleConsloe">
    <classes>
      <class name="GoogleConsole"/>
    </classes>
  </test>
</suite>

Step 3: Copy selenium standalone server, TestNG.jar, .class and testing.xml files to a folder(D:\Google\)
Step 4: Open command prompt and change to D:\Google\
Step 5: Set path using the following command
set classpath=%classpath%;D:\Google\selenium-server-standalone-2.39.0.jar;D:\Google\testng-6.8.jar;.;

*Note: update the paths of the .jar files and .class file according to your path.

Step 6: Run the class file using the following command

java org.testng.TestNG java org.testng.TestNG testng.xml

Now you can see the bowser will open and perform the required operation without code and eclipse
 

Monday, 2 December 2013

Classpath Setup for Java - Windows

Purpose: Classpath Setup for Java - Windows 7

We all know the importance of CLASSPATH for Java. Setting PATH/CLASSPATH is very easy but some of us facing difficulties. For them I want to contribute from my blog. Following steps will provide how easy we can set CLASSPATH/PATH for Java.


Step 1: Download and install Java if not available on your box. You can download from the following URL:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 2: By default JDK will be install at "C:\Program Files\Java\jdkxxx.xx" location only. Unless we change the location at the time of installation this is the default location.

Step 3: Go to JDK installed location and you will find bin folder and open that one. copy the bin folder path.




Step 4: Right Click on My Computer and Select "Properties". Once the Properties window is opened click on Advanced System Settings.

Step 5: Once Advance System Settings window is opened click on Environment Variables option



Step 6: Once Environment Variables window is opened click on New under System Variables and enter Variable Name as PATH and Variable Values  as Path of Java Bin Folder(previously copied path) and click OK




Step 7: Go to JDK folder -> open lib folder and copy the path

 


Step 8: Repeat the steps 4 and 5. Now click on New under User Variables and enter User Variable as CLASS and Variable Values as Path of the lib folder and click on OK




Step 9:  Click on OK of Environment Variables window and System Properties window

Step 10: Open Command Prompt and run any java program and you are able to successfully complete.