#Array_index_out_of_bound
4. Automation Program to login into a webpage
package webdrivercommand;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Loginwebpage
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C://Selenium//chromedriver.
exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.google.com/");
Thread.sleep(5000);
driver.close();
}
}
5. An automation program to check whether a test case has passed or failed
package webdrivercommand;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
Public class Login
{
Public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C://Selenium//chromedriver.
exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.way2sms.com/");
Thread.sleep(5000);
driver.findElement(By.id("mobileNo")).sendKeys("9700339582");
Thread.sleep(5000);
driver.findElement(By.name("password")).sendKeys("hanuman");
Thread.sleep(5000);
driver.findElement(By.xpath("//b[@id='mainErr']/following::*[5]")).click();
Thread.sleep(5000);
String o="http://www.way2sms.com/send-sms";
String z=driver.getCurrentUrl();
if(z.equals(o))
{
System.out.println("Login Sucessful_Test Passed");
}
else
{
System.out.println("Login UnSucessful_TestFailed");
}
driver.close();
}
}
6.A program to read the contents of an excel file and printing the contents on the
selenium output console using jxl
package Webdrivercommand;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcelFile
{
public void readExcel() throws BiffException,IOException
{
String Filepath="C:\\Users\\Madhwaraj\\Desktop\\sampledoc1.xls";
FileInputStream fs=new FileInputStream(Filepath);
Workbook wb = Workbook.getWorkbook(fs);
Sheet sh=wb.getSheet("Sheet1");
int total_no_of_rows=sh.getRows();
int total_no_of_columns=sh.getColumns();
for(int row=0;row<total_no_of_rows;row++)
{
for(int col=0;col<total_no_of_columns;col++)
{
System.out.print(sh.getCell(col,row).getContents() +"\t");
}
System.out.println(); } }
public static void main(String args[]) throws BiffException,IOException
{
ReadExcelFile DT=new ReadExcelFile();
DT.readExcel();
}
}
7. Program to count the total number of hyperlink objects present on a webpage
package webdrivercommand;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Totalnoofobjects
{
Public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver.ex
e");
WebDriver driver = new ChromeDriver();
Thread.sleep(5000);
driver.get("http://demo.guru99.com/test/newtours//");
Thread.sleep(5000);
List<WebElement>e = driver.findElements(By.tagName("a"));
System.out.println(e.size());
for(inti=0; i<e.size(); i=i+1)
{
System.out.println(e.get(i).getText());
}
driver.close();
}
}
8. Program to count the total number of objects present in a list (or) combo box
package webdrivercommand;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Totalnoofobjectscombobox
{
Public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C://Selenium//chromedriver.ex
e");
WebDriver driver=new ChromeDriver();
driver.get("http://demo.guru99.com/test/newtours//");
Thread.sleep(5000);
driver.findElement(By.linkText("REGISTER")).click();
Thread.sleep(5000);
driver.findElement(By.name("firstName")).sendKeys("Madhwaraj");
Thread.sleep(5000);
driver.findElement(By.name("lastName")).sendKeys("Prof");
Thread.sleep(5000);
driver.findElement(By.name("phone")).sendKeys("9703336699");
Thread.sleep(5000);
driver.findElement(By.id("userName")).sendKeys("abcd@gmail.com");
Thread.sleep(5000);
driver.findElement(By.name("address1")).sendKeys("chennaifasf");
Thread.sleep(5000);
driver.findElement(By.name("city")).sendKeys("chennai");
Thread.sleep(5000);
driver.findElement(By.name("state")).sendKeys("TN");
Thread.sleep(5000);
driver.findElement(By.name("postalCode")).sendKeys("5000221");
Thread.sleep(5000);
Select s=newSelect((driver.findElement(By.name("country"))));
List listOptionDropdown = s.getOptions();
9. Switching between web pages and performing certain actions using Mozilla
web browser
package Webdrivercommand;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Mozilla
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C://Madhwaraj//geckodriver.exe
");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.google.com/");
String baseurl=driver.getCurrentUrl();
System.out.println(baseurl);
Thread.sleep(5000);
driver.navigate().to("https://seleniummaster.com/llc/");
String tourl=driver.getCurrentUrl();
System.out.println(tourl);
Thread.sleep(5000);
driver.navigate().back();
String backurl=driver.getCurrentUrl();
System.out.println(backurl);
Thread.sleep(5000);
driver.navigate().refresh();
String refreshurl=driver.getCurrentUrl();
System.out.println(refreshurl);
Thread.sleep(5000);
driver.navigate().to("https://seleniummaster.com/llc/");
String tourl1=driver.getCurrentUrl();
System.out.println(tourl1);
Thread.sleep(5000);
driver.manage().window().maximize();
Thread.sleep(5000);
driver.close();
}
}
10. A test program to automate the registration page of any e-commerce website
package Webdrivercommand;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Register_ecommerce
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C://Selenium//chromedriver.ex
e");
WebDriver driver=new ChromeDriver();
String URL="http://automationpractice.com/index.php";
driver.get(URL);
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
driver.manage().window().maximize();
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.cssSelector("[name='email_create']")).sendKeys("mk34
8585@gmail.com.com");
driver.findElement(By.xpath("//button[@name=\"SubmitCreate\"]")).click();
driver.findElement(By.xpath("//input[@id=\"id_gender1\"]")).click();
driver.findElement(By.name("customer_firstname")).sendKeys("Test User");
driver.findElement(By.name("customer_lastname")).sendKeys("Vsoft");
driver.findElement(By.id("passwd")).sendKeys("PKR@PKR");
driver.findElement(By.id("firstname")).sendKeys("Test User");
driver.findElement(By.id("lastname")).sendKeys("Vsoft");
driver.findElement(By.id("company")).sendKeys("Vsoft");
driver.findElement(By.id("address1")).sendKeys("Test 81/1,2nd cross");
driver.findElement(By.id("city")).sendKeys("XYZ");
WebElement statedropdown=driver.findElement(By.name("id_state"));
driver.findElement(By.name("postcode")).sendKeys("51838");
WebElement countrydropDown=driver.findElement(By.name("id_country"));
Select oSelectC=new Select(countrydropDown);
driver.findElement(By.id("phone_mobile")).sendKeys("234567890");
driver.findElement(By.xpath("//input[@name=\"alias\"]")).clear();
driver.findElement(By.xpath("//input[@name=\"alias\"]")).sendKeys("Office");
driver.findElement(By.name("submitAccount")).click();
userText=driver.findElement(By.xpath("//*[@id=\"header\"]/div[2]/div/div/
nav/div[1]/a")).getText();
if(userText.contains("Vsoft"))
{
System.out.println("User Verified,Test case Passed");
}
else
{
System.out.println("User Verification Failed,Test case Failed");
}
}
}
---------------------------------------------------------
Data Structure
4 . Write a C++ program that uses stack operations to convert a given infix
expression into its postfix equivalent, Implement the stack using an array
#include<iostream>
using namespace std;
//FUNCTION TO RETURN PRECEDENCE OF OPERATORS
int prec(char c)
{
if(c=='^'||c=='$')
return 3;
else if(c=='*'||c=='/')
return 2;
else if(c=='+'||c=='-')
return 1;
else
return -1; //#
}
// FUNCTION TO CONVERT INFIX TO POSTFIX EXPRESSION
void infixToPostfix(string s)
{
char st[20];
int top=-1;
string result;
for(int i=0;i<s.length();i++)
{
char c=s[i];
// IF SCANNED CHARACTER IS AN OPERAND ADD TO OUTPUT STRING
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))
result=result+c;
// IF SCANNED CHARACTER IS AN '(' PUSH TO STACK
else if(c=='(')
st[++top]='(';
// IF SCANNED CHARACTER IS AN ')' POP ALL SYMBOLS
else if(c==')')
{
while(top!=-1 && st[top]!='(')
{
char temp=st[top];
st[top--];
result=result+temp;
}
st[top--];
}
// IF AN OPERATOR IS SCANNED
else
{
while(top!=-1 && prec(s[i])<=prec(st[top])) {
char temp=st[top];
st[top--];
result=result+temp;
}
st[++top]=c;
}
}
// POP ALL REMAINING ELEMENTS FROM STACK
while(top!=-1)
{
char temp=st[top];
st[top--];
result=result+temp;
}
cout<<result<<endl;
}
// MAIN PROGRAM TO TEST ABOVE FUNCTIONS
int main()
{
string exp;
cout<<"Enter an Infix Expression:"<<endl;
cin>>exp;
cout<<"Postfix Expression:"<<endl;
infixToPostfix(exp);
return 0;
}5 . Using recursion,
a) Solving Towers of Hanoi Problem
b) Finding factorial of a given number
c) Calculation of GCD and LCM of 3 integer Numbers
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
void hanoi(int d,char source,char temp,char dest)
{
if(d==1)
{
cout<<" Move Disk "<< d <<" from "<< source <<" to "<< dest <<endl;
}
else
{
hanoi(d-1,source,dest,temp);
cout<<"\n Move Disk "<< d <<" from "<< source <<" to "<< dest <<endl;
hanoi(d-1,temp,source,dest);
}
}
int factorial(int n)
{
if(n<0)
return(-1);
if(n==0)
return(1);
else
{
return(n*factorial(n-1));
}
}
int gcd(int m,int n)
{
int r;
if(m<n)
return gcd(n,m);
r=m%n;
if(r==0)
return n;
else
return(gcd(n,r));
}int lcm(int a,int b,int n)
{
if(n%a==0 && n%b==0)
return n;
else
return(lcm(a,b,n+1));
}
int main()
{
int g,l,x,y,z,n,ch,disk,move,fact,value;
while(1)
{
cout<<"\n 1.TOWERS OF HANOI";
cout<<"\n 2.FACTORIAL OF A GIVEN NUMBER";
cout<<"\n 3.GCD & LCM";
cout<<"\n ENTER YOUR CHOICE :";
cin>>ch;
switch(ch)
{
case 1: {
cout<<"ENTER THE NO OF DISK:";
cin>>disk;
move=(pow(2,disk)-1);
cout<<"NO OF MOVES ARE:"<<move<<endl;
hanoi(disk,'A','B','C');
break;
}
case 2: {
cout<<"ENTER THE NUMBER:";
cin>>value;
fact=factorial(value);
cout<<"FACTORIAL OF A NUMBER:"<<fact<<endl;
break;
}
case 3: {
cout<<"ENTER THREE INTEGERS:";
cin>>x>>y>>z;
n=1;
g=gcd(gcd(x,y),z);
l=lcm(lcm(x,y,n),z,n);
cout<<"GCD IS:"<<g<<"\n LCM IS:"<<l;
break;
}
case 4:{
exit(0);break;
}
default:cout<<"\n INVALID CHOICE...";
}
}
return 0;
}7 .Simulating the working of circular queue.
#include<iostream>
using namespace std;
int MAX=3;
int cq[4], rear=0, front=1, count=0;
void cqins()
{
int elt;
if (count==MAX)
{
cout<<"\n cq is full";
}
else
{
cout<<"\n enter the element to insert";
cin>>elt;
rear=rear%MAX+1;
cq[rear]=elt;
count++;
}
}
void cqdel()
{
if (count==0)
{cout<<"\n cq is empty";
}
else
{
cout<<"\n delete item is "<<cq[front];
front=front%MAX+1;
count--;
}
}
void cqdis()
{
int i,j;
i=front;
if(count==0)
{
cout<<"\nempty";
}else
{
cout<<"\n cq elements are :";
for(j=1; j<=count; j++)
{
cout<<"\n"<<cq[i];
i=i%MAX+1;
}
cout<<"\n front of the cq: "<< cq[front];
cout<<"\n rear of the cq:"<<cq[rear];
}}
int main()
{
int ch;
for (;;)
{
cout<<"\n cq operation";
cout<<"\n 1. Insert";
cout<<"\n 2. delete";
cout<<"\n 3. display";
cout<<"\n 4. exit";
cout<<"\n enter your choice";
cin>>ch;
switch(ch)
{
case 1: cqins();
break;
case 2: cqdel();
break;
case 3: cqdis();
break;
case 4: cout<<"exit";
break;
default: cout<<"\n invalid choice";
}
}
return 0;
}10 . Implement the techniques of Selection Sort, Insertion Sort, Quick sort
Selection Sort :
#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void selectionSort(int *array, int size) {
int i, j, imin;
for(i = 0; i<size-1; i++) {
imin = i; //get index of minimum data
for(j = i+1; j<size; j++)
if(array[j] < array[imin])
imin = j;
//placing in correct position
swap(array[i], array[imin]);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
selectionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}Insertion Sort :
#include<iostream>
using namespace std;
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void insertionSort(int *array, int size) {
int key, j;
for(int i = 1; i<size; i++) {
key = array[i];//take value
j = i;
while(j > 0 && array[j-1]>key) {
array[j] = array[j-1];
j--;
}
array[j] = key; //insert in right place
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
insertionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}
Quick sort :
// C++ Implementation of the Quick Sort Algorithm.
#include <iostream>
using namespace std;
int partition(int arr[], int start, int end)
{int pivot = arr[start];
int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
// Giving pivot element its correct position
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
// Sorting left and right parts of the pivot element
int i = start, j = end;
while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int start, int end)
{
// base case
if (start >= end)
return;
// partitioning the array
int p = partition(arr, start, end);
// Sorting the left part
quickSort(arr, start, p - 1);
// Sorting the right partquickSort(arr, p + 1, end);
}
int main()
{
int n;
cout<<"Enter the number of elements: ";
cin>>n;
int arr[n];
cout<<"Enter the elements: "<<endl;
for (int i = 0; i < n; i++) {
cin>>arr[i];
}
quickSort(arr,0,n-1);
cout<<"Sorted Array : "<<endl;
for (int i = 0; i < n; i++) {
cout<<arr[i]<<" "<<endl;
}
return 0;
}11 . Write a C++ program that uses function templates to perform the following:
a) Search for a key element in a list of elements using linear search.
b) Search for a key element in a list of sorted elements using binary search.
#include<iostream>
using namespace std;
template<class T>
void Lsearch(T *a,T item,int n)
{
int z=0;
for(int i=0;i<n;i++)
{
if(a[i]==item)
{
z=1;
cout<<"\n Item Found at Position="<<i+1<<"\n\n";
}
else
if(z!=1)
{
z=0;
}
}
if(z==0)
cout<<"\n Item Not Found in the list \n\n";
}
template<class T>
void Bsearch(T *a,T item,int n)
{
int beg=0,end=n-1;
int mid=(beg+end)/2;
while((a[mid]!=item)&&(n>0))
{
if(item>a[mid])
beg=mid;
else
end=mid;
mid=(beg+end)/2;
n--;
}
if(a[mid]==item)
cout<<"\n Item Found at Position="<<mid+1<<"\n\n";
elsecout<<"\n Item Not Found in the List"<<"\n\n";
}
int main()
{
int iarr[10]={2,42,56,86,87,99,323,546,767,886};
double darr[6]={2.4,5.53,44.4,54.45,65.7,89.54};
int iele;
double dele;
cout<<"\n Elements of integer Array\n";
for(int i=0;i<10;i++)
{
cout<<" "<<iarr[i]<<",";
}
cout<<"\n Enter an item to be searched: \n";
cin>>iele;
cout<<"\n Linear Search Method \n";
Lsearch(iarr,iele,10);
cout<<"\n Binary Search Method \n";
Bsearch(iarr,iele,10);
cout<<"\n Elements of double array \n";
for(int i=0;i<6;i++)
{
cout<<" "<<darr[i]<<" , ";
}
cout<<"\n\n Enter an item to be searched:";
cin>>dele;
cout<<"\n Linear Search Method \n";
Lsearch(darr,dele,6);
cout<<"\n Binary Search Method \n";
Bsearch(darr,dele,6);
}12 . Write a C++ program that uses functions to perform the following:
a) Create a binary search tree of integers.
b) Traverse the above Binary search tree in inorder, preorder and
postorder.
#include<iostream>
using namespace std;
struct st
{
int data;
struct st *left;
struct st *right;
};
struct st *root=NULL,*temp;
void insertElements();
void preorder(struct st *);
void inorder(struct st *);
void postorder(struct st *);
int main()
{
int ch;
while(1)
{
cout<<"\n 1.Insert Elements \n 2.Preorder \n 3.Inorder \n 4.Postorder \n 5.Exit";
cout<<"\n Enter your choice ";cin>>ch;
switch(ch)
{
case 1:insertElements();
break;
case 2:preorder(root);
break;
case 3:inorder(root);
break;
case 4:postorder(root);
break;
case 5:exit(1);
break;
default:
cout<<"Invalid operation";
}
}
}
void insertElements()
{
struct st *nc,*pNode;
int v;
cout<<"\n Enter the value ";
cin>>v;
temp=new st;
temp->data=v;temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
nc=root;
while(nc!=NULL)
{
pNode=nc;
if(v<nc->data)
nc=nc->left;
else
nc=nc->right;
}
if(v < pNode->data)
{
pNode->left=temp;
}
else
pNode->right=temp;
}}
void preorder(struct st *temp)
{
if(temp!=NULL)
{
cout<<" "<<temp->data;
preorder(temp->left);
preorder(temp->right);
}
}
void inorder(struct st *temp)
{
if(temp!=NULL)
{
inorder(temp->left);
cout<<" "<<temp->data;
inorder(temp->right);
}
}
void postorder(struct st *temp)
{
if(temp!=NULL)
{postorder(temp->left);
postorder(temp->right);
cout<<" "<<temp->data;
}
}