Sunday, 31 March 2019

Selenium: Putting Excel Data in Hashmap and Using it with @Data Provider of testng

Excel File has below structure:

username password
sumit       pwd1
neha        pwd2


package com.testcases.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class loginRediff {

@Test(dataProvider = "getdata")
public void login_Rediff(Map<Object, Object> mapdata) {

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "H:\\chromedriver (2).exe");
driver = new ChromeDriver();
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
// System.out.println("print");
System.out.println(mapdata.get("dummy"));

driver.findElement(By.xpath("//input[@name='login']")).sendKeys(mapdata.get("username").toString());
driver.findElement(By.xpath("//input[@name='passwd']")).sendKeys(mapdata.get("password").toString());
driver.findElement(By.xpath("//input[@title='Sign in']")).click();
driver.quit();
}

@DataProvider(name ="getdata")
public Object[][] dataproviderMethod() {

FileInputStream fis = null;
XSSFSheet ws = null;
XSSFWorkbook wb = null;
String filePath = "C:\\Users\\config\\git\\string\\stringstudy\\src\\main\\resources\\com\\resources\\files\\HashmapTrail.xlsx";

File xls = new File(filePath);
try {
fis = new FileInputStream(xls);
} catch (FileNotFoundException e) {
//
e.printStackTrace();
}
// XSSFWorkbook wb;
try {
wb = new XSSFWorkbook(fis);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ws = wb.getSheet("Sheet1");
int rowcount = ws.getLastRowNum();
int colcount = ws.getRow(0).getLastCellNum();
Object[][] data = new Object[rowcount][1];

for (int r = 0; r < rowcount; r++) {
// Create Hashmap after every row iteration
HashMap<Object, Object> hm = new HashMap<Object, Object>();
for (int c = 0; c < colcount; c++) {
hm.put(ws.getRow(0).getCell(c).toString(), ws.getRow(r + 1).getCell(c).toString());

}

data[r][0] = hm;
// Add every row in hashmap
}

try {
wb.close();
} catch (IOException e) {

e.printStackTrace();
}

return data;
}

}




//Maven pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stringstudy.demo</groupId>
<artifactId>stringstudy</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

</dependencies>
</project>

Tuesday, 26 March 2019

UFT Accenture Interview 2019

1. Difference between Instr and Instrrev
Ans: Syntax is slightly different

dim str
str="accenture"

'CASE 1:
msgbox Instr(1,str,t)
=>6
msgbox InstrRev(1,str,t)
=>6

'CASE 2:
msgbox Instr(1,str,"c")
=>2
msgbox InstrRev(1,str,"c")
=>3

#If  string to be searched exists only one in provided string then index will be same.
#If more than one occurrence then Instrrev will give index of string from right to left


2. Select all names which begins with "a" in excel file with column emp name using adodb

3. Adodb fields and values

4. Regular expression question

5. FSO:  from a file how many times character is repeated

6. Find how many times c appears in word "accenture"

7. Ordinal Identifiers

8. Array vs dictionary

9. Redim

10. Framework walk through

11. Estimations

12.Challenges faces so far











Tuesday, 19 March 2019

Java: Using Super()

package constructorDemo;

 public class Superclass {

public int rollno;
public String name;

Superclass(int rollnolocal, String namelocal) {

rollno = rollnolocal;
name = namelocal;

}

}



package constructorDemo;

class subclass extends Superclass {

public int classnum;
public String subject;

subclass(int rollnolocal, String namelocal,int classnumlocal,String subjectlocal) {
super(rollnolocal,namelocal);
classnum = classnumlocal;
subject= subjectlocal;

System.out.println("Roll num is:"+rollno +" Name is "+name+ "Classnum is "+classnum+" Subject is "+subject);
}

Monday, 18 March 2019

Java: File Reading with Buffered Reader

package FileHandling;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

import java.io.IOException;

public class ReadFile {

public static void main(String[] args) throws IOException {

File fl = new File("H:\\TstArea\\filetoread.txt");

FileReader fr = new FileReader(fl);

BufferedReader br = new BufferedReader(fr);

System.out.println(br.readLine());

String line = null;

while (line != br.readLine()) {

System.out.println(br.readLine());

}

}

}

Java: File Writing with BufferedWriter

package FileHandling;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

 public class FileWritertest {

public static void main(String[] args) throws IOException {


File fl=new File("H:\\TstArea\\filewriter.txt");

FileWriter fw=new FileWriter(fl,false);

BufferedWriter bfw=new BufferedWriter(fw);

bfw.write("sumit");
bfw.newLine();
bfw.write("neha");
bfw.close();




}

}

Core Java: This() and this

package StudyPackage;

public class ThisKeyword {

//Default Constructor
ThisKeyword(){
this(5);
System.out.println("Default Constructor");
}

//Constructor with 1 parameter
ThisKeyword(int data){
this(5,6);
System.out.println("Int Constructor");
}

//Constructor with 2 parameters
ThisKeyword(int data,int data1){
System.out.println("Int Constructor with 2 Arguments");

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

  ThisKeyword th=new ThisKeyword();//Calling Default
  //  
}
}

OutPut:
Int Constructor with 2 Arguments
Int Constructor
Default Constructor

#this() is useful for calling the constructor. Matching constructors gets called based on parameters provided.

#Should be first line in constructor. 2 this() not allowed


Friday, 15 March 2019

Java: Apache POI : Writing to Excel File

package poitest;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WritingExcel {

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


XSSFWorkbook wb=new XSSFWorkbook(); //new workbook 
XSSFSheet ws=wb.createSheet("dummy");  //Sheet Object

XSSFRow row0=ws.createRow(0); //Row Creation
XSSFRow row1=ws.createRow(1);
XSSFRow row2=ws.createRow(2);


XSSFCell cell0=row0.createCell(0); //Cell Access
XSSFCell cell1=row1.createCell(0);
XSSFCell cell2=row2.createCell(0);

cell0.setCellValue("0"); //Actual writing
cell1.setCellValue("1");
cell2.setCellValue("2");

FileOutputStream fio=new FileOutputStream("H:\\TstArea\\writewithpoi.xlsx");
               //File Creation for writing output

wb.write(fio); //Writing
wb.close(); //Close Workbook

}

}


//MAVEN APACHE POI DEPENDENCIES
<dependencies>
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.1</version>
</dependency>
 </dependencies>


Tuesday, 5 March 2019

Selenium Keyboard Compoud Actions and Javascript executor

package TestCases;

import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.Assert;

public class mouseKeyboard {


    @Test
    public void mousekbddemo() {
   
    System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
        WebDriver driver= new ChromeDriver();
       
        String baseUrl = "http://www.facebook.com/";
        driver.get(baseUrl);
        driver.get(baseUrl);
        WebElement txtUsername = driver.findElement(By.id("email"));

//Series of Steps
//        Actions builder = new Actions(driver);
//        Action seriesOfActions = builder
//        .moveToElement(txtUsername)
//        .click()
//        .keyDown(txtUsername, Keys.SHIFT)
//        .sendKeys(txtUsername, "sumit")
//        .keyUp(txtUsername, Keys.SHIFT)
//        .doubleClick(txtUsername)
//        .contextClick()
//        .build();
//        seriesOfActions.perform() ;
       
        
        //Writes SSSS in email textbox on Facebook in single step
        Actions a=new Actions(driver);
 a.moveToElement(txtUsername).click().keyDown(Keys.SHIFT).sendKeys("ssss").build().perform();
       
        
        //getText for email textbox returns empty value.
       
   
        //Using Java Script Executor to get the entered value i.e SSSS
        JavascriptExecutor js= (JavascriptExecutor)driver;
        String return_value = (String) js.executeScript("return     document.getElementById('email').value");
        System.out.println(return_value);
 
      Assert.assertEquals(return_value, "SSSS", "Comparsing values post data entry");
     
       
     
    }
   


}

Monday, 4 March 2019

Selenium Javascript and Alert Handling

package TestCases;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class JavaScriptExecutor {



@Test
    public void Login() throws InterruptedException
    { System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
        WebDriver driver= new ChromeDriver();
       
        //Creating the JavascriptExecutor interface object by Type casting
        JavascriptExecutor js = (JavascriptExecutor)driver;
       
        //Launching the Site.
        driver.get("http://moneyboats.com/");
   
        //Maximize window
        driver.manage().window().maximize();
       
        Thread.sleep(5000);
       
        //Vertical scroll down by 600  pixels
        js.executeScript("window.scrollBy(0,600)");
       
        //Adding javascript alert to webpage
        js.executeScript("window.alert(\"sometext\")");
       
       
        System.out.println(driver.switchTo().alert().getText()); //sometext
        //Accepting the Alert
        driver.switchTo().alert().accept();
    }
}

Spring Boot : Exception Handler 14