Monday, 15 May 2017

Java - Reading the text content from file with out loop through

Reading the text content from file with out loop through.

Sample Program:
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ReadFile {
public static void main(String[] args) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get("C:\\filename.txt"));
String fileContent = new String(encoded, Charset.defaultCharset());

System.out.println(fileContent);
}
}


Reading the text content from File using 'while' loop.

Sample Program:
import java.io.BufferedReader;
import java.io.FileReader;

public class Read2File {
public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("C:\\filename.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String fileLine = null;
StringBuffer temp = new StringBuffer();
while((fileLine = bufferedReader.readLine()) != null) {
                              temp.append(fileLine+"\n");
                        }
System.out.println(temp);
bufferedReader.close();
   fileReader.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}

Monday, 1 May 2017

Java - Reading the text content(xml/json) with out loop through 'InputStreamReader'

Reading the text content(xml/json) with out loop through 'InputStreamReader'.

Prerequisite:
Requires java 8 to execute below program

Sample Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.stream.Collectors;

public class Test8WsGet {

public static void main(String[] args) {
try {

URL url = new URL("http://thomas-bayer.com/sqlrest/CUSTOMER/2/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");

if (connection.getResponseCode() != 200) {
throw new RuntimeException("Failed with HTTP error code : " + connection.getResponseCode());
}

String responseXml;
System.out.println("Response xml from Server .... \n");

BufferedReader bufferReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
responseXml = bufferReader.lines().collect(Collectors.joining("\n"));

System.out.println(responseXml);

connection.disconnect();
} catch (Exception e) {
e.printStackTrace();

}
}
}
Troubleshooting :
If you are executing above program using maven(i.e. pom.xml) then add below maven-compiler-plugin
Reference: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>


Reading the text content(xml/json) from 'InputStreamReader' using 'while' loop.

Sample Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestWsGet {

public static void main(String[] args) {
try {

URL url = new URL("http://thomas-bayer.com/sqlrest/CUSTOMER/2/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");

if (connection.getResponseCode() != 200) {
throw new RuntimeException("Failed with HTTP error code : " + connection.getResponseCode());
}

String responseXml;
System.out.println("Response xml from Server .... \n");

BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
while ((responseXml = br.readLine()) != null) {
System.out.println(responseXml);
}

connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Saturday, 7 January 2017

VBScripts Fundamentals - Part1


Using concatenate, "&_" and "vbcrlf" in VB Script

Watch Video
 
Prerequisite:
http://automation-home.blogspot.in/2017/01/vb-script-basic-example.html
 
Using concatenate, "&_" and "vbcrlf" in VB Script
- Concatenate using plus sign(+) and ampersand (&).

- String in multiple lines using "&_" and ''+_''.
   ex: Sql query as string

- To print in different lines use "vbcrlf" (it is like '\n').
    ex: "Hello welcome" --> "Hello" &vbcrlf& "welcome"
    References: http://powerasp.net/content/new/vbscript-constants.asp 
                        http://www.asciitable.com/

Thursday, 5 January 2017

VB Script Basic Example

Watch Video


Writing the VB Script in
- Notepad
- Notepad++
      https://notepad-plus-plus.org/download/v7.3.html
- VbsEdit
      http://www.vbsedit.com/

Saving the file
Save the file  as <<File Name>>.vbs

Execution the VB Script.
- Command Prompt
- Editor
- Double click on VB Script file

Saturday, 31 December 2016

C# Xamarin Studio - SpecFlow configuration, creating sample feature file, step defintion and execution.

Prerequisite:
Download and install latest version of Xamarin Studio from http://www.monodevelop.com/download/

Download SpecFlow addin for Xamarin Studio 6 from

Xamarin Studio addin for SpecFlow is downloaded.

Open the Xamarin Studio and 
select 'Tools' -> 'Add-ins'

Click 'Install from file' button.

Open the location of file downloaded(i.e. specflow addin file 'TechTalk.SpecFlow.TechTalk.SpecFlow_1.9.1.0.mpack')
   Click 'Open' button.

 Click 'Install' button.

Open the Xamarin Studio, Create the new solution

 New Project dialog is opened,
    select 'Other > Miscellaneous > General > SpecFlow NUnit Library Project' to select SpecFlow template for project.

Enter project name and click 'Create' button.
   ex- project name: FirstSpecFlowTest  
   
Feature file is created(i.e. default feature file is created)

Select 'View' -> 'Design' to view solution explorer.

Add package 'Xamarin.UITest' from Nuget package to the project.  
       Search for 'Xamarin.UITest', select checkbox for 'Xamarin.UITest' and click 'Add Package'.

'Xamarin.UITest' is installed and added to project package.

Select 'View' -> 'Test' to open 'Unit Test Window'

 If 'Unit Test' pad is not opened, then select 'View' -> 'Pads' -> 'Unit Tests' to view 'Unit Test' pad.

Two files are present(i.e. created)
     Test.feature -> Feature file
     TestSteps.cs -> Step definitions file

Build Project

Run Test in 'Unit Test'

Test Result

Add Console.Writeline(....) in step definition methods.
    Note: Console.Writeline(...) is added for only test demo purpose, in real time there will be some logic/implementation is added instead of Console.Writeline(...) in step definition methods.

Build the project and run the test again

Open the 'Application Output', by selecting 'View' -> 'Pads' -> 'Application Output'
    View the output of Console.Writeline(....) in 'Application Output'     

References:  

C# Visual Studio - SpecFlow Step Definition file

Prerequisite:
Feature file is already created.
Creating the feature file: https://automation-home.blogspot.in/2016/12/c-visual-studio-specflow-feature-file.html


Create the class for step defintions

 Enter the class name and click 'Add' button.
   ex: SampleFirstFeatureSteps.cs
       As a best practice, follow file name ends with 'Steps' <<FileName>>Steps.cs

Class file for step definitions is created.

Mouse right click on step in feature file and select the 'Generate Step Definitions'.

Click 'Preview' button.

Step definition skeleton preview is displayed, close the preview.

Copy the methods(i.e. step definitions methods)

Open the step definitions class file(i.e. 'SampleFirstFeatureSteps.cs'),
   paste the methods(i.e. step definitions) in class   

Since references are missing, shows compilation error for the methods.

Add the missing references by clicking (Ctrl+.)  

Open the feature file, right click on the step and select 'Go To Definition'.

Click 'No' button to close.
There is no binding between Feature file and Step Definitions.
Since there is no binding above dialog window is displayed.

Add the attribute [Binding] for class in Step Definition file.

Remove Unncessary Usings.

Observe the colour for steps in feature file, before and after adding [Binding] attribute for Step Definition class.
Open the Feature file, mouse over on the step right click and select 'Go To Definition'.
    This time when 'Go To Definition' is selected, Steps defintion file is opened(i.e Binding of feature file and step defintion file is successfull).

As a best practice place the feature file in 'Features' folder and step definitions file in 'Step_Definitions' folder.

<< Previous