In this tutorial we will focus on executing java programs for
practice without using IDE’s like Eclipse,
NetBeans,IntelliJ and also to run java programs
without installing the JAVA Software and compile
and run using the Online Editors.
Download and install the Java from this link -> Java SE Development Kit 8 Downloads
Executing the JAVA Programs without using IDE’s such as Eclipse,Net-beans, IntelliJ Editors
Make sure the java is installed in the location.
JAVA INSTALLATION DIRECTORY LOCATION
Basic Program in JAVA.
HelloWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
publicclassHelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
/* Commenting the below lines of code..
Objective: To print the Message "Hello World" in the console.
Points to be Noted:
------------------
ClassName: HelloWorld
{}- Curly Braces
()- paranthesis
[]- Square Brackets
; -Semi colon
"" - Double Quotes
. - period
*/
Compiling Java from the Command Line / Terminal on MAC or Linux.
1. To open command line Win + R keys on your keyboard. Then, type cmd or cmd.exe and press Enter
Win + R keys on your keyboard. Then, type cmd or cmd.exe and press Enter to open Command line.
2. Execute the following Command to Compile the Program.
javac HelloWorld.java
javac not recognized error: ‘javac’ is not recognized as an internal or external command, operable program or batch file error
comes when you try to compile a Java source file using javac command e.g. javac Helloworld.java
To overcome this you can give the path of the javac available in the program files.
Location of file javac available under the directory java/jdk.
Executing Java from the Command Line / Terminal on MAC or Linux.
open command line and execute the JAVA program using the follow command.
java HelloWorld
Executing the JAVA programs using Online Editors without downloading the software.
Practicing the java programs without installing the Java is easy using the below online
editor. This helps to Edit and execute the programs
Pre-requisites
Follow the Steps in the Link to download the sample REST Application and deploy on
a tomcat Server-> REST API Application Live Example Implementing the Framework
All the below methods taken care as part of Framework
1. Method Response getServiceResponse(String serviceURL) added as part of the RestLibrary.java class which helps to provide the response based on the end point URL passed as a parameter.
REST API - GET Method Example
1
2
3
4
5
6
7
public static Response getServiceResponse(String serviceURL)throwsException
{
RequestSpecification request=RestAssured.given();
request.headers(provideHeaders());
Response response=request.when().get(serviceURL);
return response;
}
2. Methods used to store the Run time Value in setSysProperty() and fetch the value using getSysProperty().
REST API - GET Method Example
1
2
3
4
5
6
7
8
9
10
11
12
public static String getSysProperty(String propName){
String propValue=System.getProperty(propName);
return propValue!=null?propValue:null;
}
public static void setSysProperty(String propName,String propValue){
if(propValue!=null){
System.setProperty(propName,propValue);
}else{
System.setProperty(propName,(String)null);
}
}
3. This framework is implemented with Object Properties where the REST Endpoint url’s are
stored in the configuration.properties.
Sorting:
In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order or alphabetical order.
In Selenium WebDriver it is important to verify the Sort Functionality of the Dynamic HTML Content
is working as expected.
Sorting the Dynamic HTML Table-Selenium WebDriver & Java
Steps to Perform:
1. Retrieve the List from HTML Table.
2. Store the List in an Array
3. Sorting the items in the Array using Swapping. Swapping is the process of Exchanging the Values.
4. Click on Sort button in the WebPage.
5. Retrieve the List again.
6. Compare the Sorted Array generated in Step 3 with the List generated in Step 5.
JMETER
The Apache JMeter™ application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.
Apache JMeter features include:
Ability to load and performance test many different applications/server/protocol types:
• Web – HTTP, HTTPS (Java, NodeJS, PHP, ASP.NET, …)
• SOAP / REST Webservices
• FTP
• Database via JDBC
• LDAP
• Message-oriented middleware (MOM) via JMS
• Mail – SMTP(S), POP3(S) and IMAP(S)
• Native commands or shell scripts
• TCP
• Java Objects
Scenario:
Execute the Selenium Scripts in JMETER and verify the average response time taken in the execution of Selenium Scripts.
1. Download the Apache JMETER zip file from this link -> https://jmeter.apache.org/download_jmeter.cgi Example: Download apache-jmeter-x.0.zip file. 2. Unzip the file and verify the contents of the JMETER.
Example: Download jpgc-webdriver-2.3.zip file. 4. Download the Java Native Access jars and JNA Platform jars and extract the files and copy in the JMETER/lib folder as shown below: Java Native Access[JNA] allows you to call directly into native functions using natural Java method invocation. The Java call looks just like the call does in native code. Most calls require no special handling or configuration. 5. Open JMeter bat available under JMeter/bin folder.
• Right Click on Test Plan->Create Thread Group.
• Right Click on Thread Group->Select Sampler->WebDriver Sampler
• Right Click on Thread Group-> Select Config Element->Firefox Driver Config.
• Right Click on Thread Group->Listener->View Results in Table. Sampler->Samplers tell JMeter to send requests to a server and wait for a response. Config->Configuration elements can be used to set up defaults and variables for later use by samplers. Listeners->A listener is a component that shows the results of the samples. 6. Copy the Jmeter Script in the WebDriver Sampler as shown below:
Selenium-JMeter Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
WDS.sampleResult.sampleStart();//captures sampler's start time
WDS.sampleResult.getLatency();
WDS.log.info("Sample started");
// 1. Open the Web Application
WDS.browser.get('http://indeed.co.in')
// 2. Enter characters into the text input field
var pkg=JavaImporter(org.openqa.selenium)
var what=WDS.browser.findElement(pkg.By.id('what'))
what.clear()
what.sendKeys(['Selenium'])
var where=WDS.browser.findElement(pkg.By.id('where'))
where.clear()
where.sendKeys(['Bangalore'])
// 3. Click on the button to submit the form
var button=WDS.browser.findElement(pkg.By.id('fj'))
button.click()
// 4. Verify successful form submission
var results=WDS.browser.findElements(pkg.By.cssSelector('.summary'))
if(results.empty){
WDS.sampleResult.successful=false
WDS.sampleResult.responseMessage='There were no results returned'
}
WDS.sampleResult.sampleEnd();
7. Execute the Jmeter Script and verify the Results in the View Results Table Listener for the Sample Time captured for executing the Selenium Script.
In Web Applications the data in the HTML Table is dynamic always.
To Fetch the data available in the Dynamic Table or to identify the
Dynamic Web Elements available in the Web Page we should depend on Xpath Axes Locators.
Following are the Xpath Axes Locators:
Link for XPATH AXES-> https://www.w3schools.com/xml/xpath_axes.asp
• ancestor – select parent or grand parent
• following-sibling – Which follows
• preceding-sibling – Which precedes
• descendant – Child/subChild
• parent – Select Immediate Parent
• child- Select immediate Child
• following – Select all the matching nodes which are following
• preceding – Select all the matching nodes which are preceding
Identify the following Xpath in Firepath for understanding: Xpath Example 1: Identify the Parent of the WebElement //a[contains(text(),'Adani Enterprises')]/parent::td
Xpath Example 2: Identify the Ancestor of the Web Element //a[contains(text(),'Adani Enterprises')]/parent::td
Xpath Example 3: Identify the following Elements of the Web Element
In the Case of following all the Elements which are matching will be selected. Now the matching nodes are 501. //a[contains(text(),'Adani Enterprises')]/parent::td/following::td
Xpath Example 4: Identify the following siblings of the Web Element
In the Case of following-sibling all the Elements which are matching but should be part of the parent tag ‘tr’. Now the matching nodes are 4. //a[contains(text(),'Adani Enterprises')]/parent::td/following-sibling::td
Fetching the Price associated with the Stock: //a[contains(text(),'Adani Enterprises')]/parent::td/following-sibling::td[2]
In Eclipse whenever we create a project automatically it will pick the latest jre installed in your system jre 1.8.0_25 which is in my case as shown below:
But in selenium webdriver when we use ‘sendkeys()’ method throws error java.lang.CharSequence cannot be resolved.
We can resolve this in two ways 1. Setting the Source level project should be greater then 1.5.
2. Roll back the jre 8 to jre 7 in Eclipse.
Steps for Setting the Source level of the Project to greater then 1.5
1. Select Project right click->Build Path->Configure Build Path.
2. Select the Java Compiler option as shown below:
3. Select check-box for ‘Enable project specific settings”. Change the value from the compliance level drop down from 1.4 to 1.7. Click on ‘Ok’ button.
4. Verify the program now the issue will be resolved Steps for roll-backing from jre 8 to jre 7
1. Select Project right click->Build Path->Configure Build Path.
2. Navigate to Libraries. Select the jre 1.8 and click on remove.Do not worry we are not removing the jre 1.8 from the system, we are removing the jre 1.8 reference from this project.
3. Click on Add Library Button->JRE System Library->Next.
4. Select Alternate JRE radio button. Click on ‘Installed JREs’ button.
5. In the installed JREs click on Add and Navigate to the directory where the jre7 is installed.
6. Select the JRE Type as ‘Standard VM’ and click on Next.
7. In my case JRE7 is installed in the following location.
C:\Program Files (x86)\Java\jre7
Provide JRE Name -> jre7.0
Click on Finish button.
8. Select jre7.0 checkbox in the list of Installed JRE’s. Click on Ok button.
9. Select the workspace default jre as JRE 7.0. Click on Finish button.
10. Verify the Java Build Path.
11. Verify the program now the issue will be resolved.