Reading And Writing File in JAVA- Usage of File Classes FileReader,BufferedReader,FileWriter,BufferedWriter

8:31 PM 1 Comments

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadWriteFileEx {

/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub

File f = new File("myFile.txt");
System.out.println(f.getAbsolutePath());
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("myFile1.txt");
BufferedWriter bw = new BufferedWriter(fw);

String str;
int count=0;
while((str=br.readLine())!=null)
{
System.out.println(str);
bw.write(str);
bw.write("\n");
if(str.contains("Selenium"))
{
count++;
}
}
System.out.println("No of times found String Selenium is "+ count);
  //Closing the Resources
bw.close();
fw.close();
fr.close();
br.close();
f=null;
}

}

1 comments:

Generate XSLT Report+ Ant build.xml+ Resolving Unknown file:23:146: Fatal Error! Could not find function: if Issue +No suites, classes, methods or jar file was specified

1:20 AM 0 Comments






We usually depend on Testng for generating the default reports. Apart from that we use XSLT reports as well to generate PIE Charts based on Failed,Passed,Skipped Testcases.
During this process we face lot of issues. Few of the most commonly observed issues are as follows:
[xslt] Unknown file:23:146: Fatal Error! Could not find function: if
[xslt] : Fatal Error! Fatal error during transformation Cause: Fatal error during transformation
In order to resolve this we have to follow the below steps. At every step we will check and see whether issues are resolved or not.
Step 1 :
Make sure the build.xml file contains 'SaxonLiaison' processor added.
Example:<xslt in="${outputdir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${outputdir}/testng-xslt/index.html"  processor="SaxonLiaison">

Re-run the build.xml and verify the issue is resolved. If the issue is not resolved goto Step2.

Step 2 :
Make sure the following jars added to the lib folder and Project build path as well.
saxon-8.7.jar -> Link to download the saxon jar-> Click here
SaxonLiaison.jar ->Link to download the SaxonLiaison jar->Click here
guice-3.0.jar->Link to download guice-3.0.jar->Click here
Re-run the build.xml and verify the issue is resolved. If the issue is not resolved goto Step3.
Step 3:
Re-verify the build.xml available in the project directory by downloading from this link Click here .Try to do necessary changes.
Re-run the build.xml and verify the issue is resolved. If the issue is not resolved goto Step3.
Step 4:
Re-verify the testng-results.xsl available in the project directory by downloading from the link Click here.Try to do necessary changes.
Re-run the build.xml and verify all the issues are resolved.
If even after the issues are not resolved, please refer to the directory structure mentioned in the below screenshot.
Resolving "No suites, classes, methods or jar file was specified" in Ant TestNG- Make sure the testng.xml is availble in the src folder as mentioned in the image below. Refer to the point1.











0 comments:

Implementation of Testng @DataProvider using EXCEL (Data Driven Framework) & JXL to Read XLS

11:07 PM 1 Comments

@DataProvider
In order to store the multiple set of input data we use @DataProvider.
We are going to read the xls file which contains input data and store in @DataProvider.
Steps to Achieve :
  a. Store the input data in an xls file.
  b. Read the data from the xls using an third party jar jxl2.6.jar
   Link for Downloading JXL Jar ->http://www.findjar.com/index.x?query=jxl
   jxl stands for -> java excel library
 c. Add the jar to the build path.
 d. Store the data in @Dataprovider
 e. Use the @Dataprovider in the @Test methods.
JExcel API ->http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/
  WorkBook contains sheets
  Sheets contains rows and column
  Each and every values stored in the rows and columns in a cell.
  Retrieve the contents of the cell using row index and column index and store in an array
Program:
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class ReadExcelJXL {
@Test(dataProvider="DP")
public void sales_coupon_count(String username,String password)
{

System.out.println("The value of username" + username);
System.out.println("The value of password" + password);

}
@DataProvider(name="DP")
public String[][] readExcel(  )
{
File file = new File("inputData.xls");
String inputData[][] = null;
Workbook w;

try {
w = Workbook.getWorkbook(file);

// Get the first sheet
Sheet sheet = w.getSheet(0);

int colcount=sheet.getColumns();

int rowcount=sheet.getRows();

inputData= new String[rowcount][colcount];

for ( int i=0;i<sheet.getRows();i++)
{
for (int j=0;j<sheet.getColumns();j++)
{
Cell cell=sheet.getCell(j,i);
inputData[i][j]=cell.getContents();
}
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputData;
 }
}
Output
The value of username-->xyz
The value of password-->abc
The value of username-->abc
The value of password-->welcome
The value of username-->ab1123
The value of password-->welcome123
PASSED: sales_coupon_count("xyz", "abc")
PASSED: sales_coupon_count("abc ", "welcome ")
PASSED: sales_coupon_count("ab1123 ", "welcome123")

===============================================
    ReadExcelJXL
    Tests run: 3, Failures: 0, Skips: 0
===============================================


===============================================
DataDriven
Total tests run: 3, Failures: 0, Skips: 0
===============================================
inputData.xls






1 comments:

Using Selenium Grid with Two Nodes-FF & Chrome(HUB)

6:35 AM 1 Comments


Steps for Setting Up the Grid

1. Download the Selenium Sever Jar File using the link

2. Pre-requisite for Every Machine 
Copy  the Selenium Server in every host hub,remote node1,remote node2

3. To check the default port number used by Hub '4444' is reserved or not
Command --> netstat -a -n | findStr 4444
We have to check this command only in hub machine.

4. Starting the Hub. Navigate to the location where the selenium-server jar is downloaded and execute the below command.
java -jar selenium-server-standalone-2.44.0.jar  -role hub

5. Starting the Hub using particular port. If the default port '4444' is already in use. Then we have to pass another port number in the command line for the argument -port as mentioned below:
java -jar selenium-server-standalone-2.44.0.jar  -role hub -port 4545

6. Verifying the Hub Console
7.Starting Node1: Navigate to the location where selenium server jar is located and execute the below command
java -jar selenium-server-standalone-2.44.0.jar  -role webdriver -browser "browserName=firefox,version=35,maxInstances=1,platform=WINDOWS" -hubHost localhost -port 4546

8. Starting Node2: Navigate to the location where selenium server jar is located and execute the below command
java -jar  selenium-server-standalone-2.44.0.jar -role webdriver -browser "browserName=chrome,version=40,maxInstances=1,platform=WINDOWS" -hubHost localhost -port 4547 -Dwebdriver.chrome.driver=D:\\Selenium\\chromedriver_win32\\chromedriver.exe 

9. Selenium WebDriver Script for FF
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("firefox");
caps.setVersion("35");
caps.setPlatform(Platform.WINDOWS);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);

10. Selenium WebDriver Script for CH
 DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
caps.setVersion("40");
caps.setPlatform(Platform.WINDOWS);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);

Selenium WebDriver Program:

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.Platform;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class GridTests {
RemoteWebDriver  driver;
DesiredCapabilities caps;
@Parameters({"browsertype","url"})
@BeforeTest
public void invokebrowser(String browserType,String url) throws MalformedURLException
{
if(browserType.equals("FF"))
{
caps = DesiredCapabilities.firefox();
caps.setBrowserName("firefox");
caps.setVersion("10");
caps.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
System.out.println("in FF");
//driver =new FirefoxDriver();
}
else if(browserType.equals("IE"))
{
System.out.println("in IE");
File f =new File("IEDriverServer.exe");
System.setProperty("webdriver.ie.driver",f.getAbsolutePath());
driver = new InternetExplorerDriver();
}
else
{
caps = DesiredCapabilities.chrome();
caps.setBrowserName("chrome");
caps.setVersion("40");
caps.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);

System.out.println("in CH");
}
driver.get(url);
}
@Test
public void verifyTitle()
{

Assert.assertEquals(driver.getTitle(),"Facebook");

}
}




1 comments:

Handling Security Authentication Pop-up Using Autoit-Selenium WebDriver!!!

10:02 PM 1 Comments

Implementation of AUTO IT in Selenium
Pre-Requisites: Download the AutoIT Full Installation and AutoIT Script Editor from the url AutoIT Website  http://www.autoitscript.com/site/autoit/downloads/
Download both the AutoIt Window Identification and Editor


Handling HTTPS pop-up using Auto it->

Url to access-> http://www.engprod-charter.net/

Step 1- Open Autoit Editor and write the following script

 Step2-Write the below program and Save the program.
$title= "Authentication Required"
WinWaitActive($title)
send("sudheer")
send("{TAB}")
send("welcome")
send("{ENTER}")
  
Step3- Compile the Script which generates the .exe file.
Step4-Execute the Script
Step5- Implement this selenium.
Runtime.getRuntime().exec("Absolute path to the https autoit script ");

1 comments:

Flickering issue of IE browser while taking screenshot in Selenium WebDriver Handled Using java.awt.Robot Class

9:53 PM 0 Comments


import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/*
 * 
 * Taking a screenshot using Robot class in java rather than 
 * TakesScreenshot Interface in Selenium Webdriver.
 *
 */

public class CaptureFullScreen {

public static void main(String[] args) throws AWTException {
try 
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

//Implementing the Robot Class
Robot robot = new Robot();
String format = "jpg";  
String imgFileName= new SimpleDateFormat("mm-dd-yyyy_HH-ss").
format( new GregorianCalendar().getTime());
System.out.println(new GregorianCalendar().getTime());

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File("d:\\"+imgFileName+".jpg"));

System.out.println("Screenshot Saved in today's date format in d:\\");
catch (  IOException ex)
{
System.err.println(ex);
}
}
}

0 comments:

How to select an item in the ListBox in Selenium Webdriver unordered (bulleted) list!!!!

7:05 AM 0 Comments

Handling the Listbox in Selenium WebDriver in the below website http://www.cheapflights.com/ is shown below:







HTML <ul> Tag
The <ul> tag defines an unordered (bulleted) list.
Use the <ul> tag together with the <li> tag to create unordered lists.
References->http://www.w3schools.com/tags/tag_ul.asp
HTML Code for ListBox in http://www.cheapflights.com/: 
<ul class="ui-dropdown-list">
<li class="ui-dropdown-selected notranslate" data-value="">1</li>
<li class="notranslate" data-value="">2</li>
<li class="notranslate" data-value="">3</li>
<li class="notranslate" data-value="">4</li>
<li class="notranslate" data-value="">5</li>
<li class="notranslate" data-value="">6</li>
<li class="notranslate" data-value="">7</li>
<li class="notranslate" data-value="">8</li>
<li class="notranslate" data-value="">9</li>
</ul>
Selenium WebDriver Code For Selecting a Value from the ListBox:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class HandlingListbox {
 
 public static void main(String[] args) {
 WebDriver driver = new FirefoxDriver();
 driver.get("http://www.cheapflights.com/");
 driver.findElement(By.cssSelector("#sb-adults>div>.arrow")).click();
 List<WebElement> liList = driver.findElements(By.cssSelector(".ui-dropdown-list>li.notranslate"));
 liList.get(3).click(); 
 }

}

Output:

0 comments:

Internet Explorer Driver - Required Configuration IE 10,IE 11

4:04 AM 0 Comments






Required Configuration

  • The IEDriverServer exectuable must be downloaded and placed in your PATH.
  • On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
  • Additionally, "Enhanced Protected Mode" must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.
  • The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
  • For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor isHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

0 comments:

How to handle https security warning in IE for Selenium WebDriver + RC & How to solve the Unhandled exception: Modal dialog present

12:53 AM 0 Comments







In Selenium Webdriver whenever we try to launch the IE we get the Security Warning pop-up.
Handling security warning pop-up can be done in two ways.

1. Using Robot Script as follows:
try {
        (new Robot()).keyPress(java.awt.event.KeyEvent.VK_ENTER);

         (new Robot()).keyRelease(java.awt.event.KeyEvent.VK_ENTER);
         } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

2. Adding the website url in the Trusted websites
  1. Open Internet Explorer by clicking the Start button Picture of the Start button. In the search box, type Internet Explorer, and then, in the list of results, click Internet  Explorer.
  2. Navigate to the website that you want to add to a specific security zone.
  3. Click the Tools button, and then click Internet Options.
  4. Click the Security tab, and then click a security zone (Local intranetTrusted sites, or Restricted sites).
  5. Click Sites.
  6. If you clicked Local intranet in step 4, click Advanced.
  7. The website should be shown in the Add this website to the zone field. Click Add.
    If the site is not a secure site (HTTPS), clear the Require server verification (https:) for all sites in this zone check box.
  8. Click Close, and then click OK (or click OK twice if you clicked Local intranet in step 4).

0 comments:

Frames.html

10:04 PM 0 Comments

<!DOCTYPE html>
<html>

<frameset cols="25%,*,25%">
  <frame src="frame_a.html" name="frame_a">
  <frame src="frame_b.html" name="frame_b">
  <frame src="frame_c.html" name="frame_c">
</frameset>

</html>

0 comments: