A place to learn Selenium-WebDriver and Core Java with real time scenarios.
Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts
Monday, July 14, 2014
Tuesday, May 27, 2014
Reverse a number or check number is palindrome or not
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number which you want to reverse: ");
int a = in.nextInt(), r=0, s=0, p=a;
while(p!=0){
r = p%10;
p = p/10;
s = s*10 + r;
}
if(s==a){
System.out.println("number entered by user "+a+" is a palindrome.");
}else
System.out.println("number entered by user "+a+" is not a palindrome.");
}
}
public class ReverseNumber {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number which you want to reverse: ");
int a = in.nextInt(), r=0, s=0, p=a;
while(p!=0){
r = p%10;
p = p/10;
s = s*10 + r;
}
if(s==a){
System.out.println("number entered by user "+a+" is a palindrome.");
}else
System.out.println("number entered by user "+a+" is not a palindrome.");
}
}
Monday, May 12, 2014
How to get the dropdown value in notepad ?
Note: FileWriter("path of the directory with filename.txt");
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
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;
import org.openqa.selenium.interactions.Actions;
public class DropdownNotepad extends Thread {
public static WebDriver driver;
public static void main(String[] args) throws IOException {
driver = new FirefoxDriver();
driver.get("http://www.etouch.net/home/index.html");
WebElement service = driver.findElement(By.xpath("//a[text()='Services']"));
Actions act = new Actions(driver);
act.moveToElement(service).perform();
List<WebElement> dropdown = driver.findElements(By.xpath("//li[@id='services']//ul//ul/li"));
System.out.println(dropdown.size());
FileWriter fileWriter = new FileWriter("C:\\selenium\\out1.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for(WebElement ele: dropdown){
System.out.println(ele.getText());
bufferedWriter.write(ele.getText()+"\n");
}
bufferedWriter.close();
driver.close();
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
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;
import org.openqa.selenium.interactions.Actions;
public class DropdownNotepad extends Thread {
public static WebDriver driver;
public static void main(String[] args) throws IOException {
driver = new FirefoxDriver();
driver.get("http://www.etouch.net/home/index.html");
WebElement service = driver.findElement(By.xpath("//a[text()='Services']"));
Actions act = new Actions(driver);
act.moveToElement(service).perform();
List<WebElement> dropdown = driver.findElements(By.xpath("//li[@id='services']//ul//ul/li"));
System.out.println(dropdown.size());
FileWriter fileWriter = new FileWriter("C:\\selenium\\out1.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for(WebElement ele: dropdown){
System.out.println(ele.getText());
bufferedWriter.write(ele.getText()+"\n");
}
bufferedWriter.close();
driver.close();
}
}
Write a program to check number is Palindrome or not / Write a program to reverse a number.
import java.util.Scanner;
class Palindrome{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("enter a number");
int n = in.nextInt();
int a = n;
int palindrome =0;
int r = 0;
while(n>0){
r = n%10;
n = n/10;
palindrome = palindrome*10 + r;
}
System.out.println("Reverse of the entered num is: "+palindrome);
if(palindrome==a){
System.out.println(a+" number is palindrome.");
}else{
System.out.println(a+" number is not palindrome.");
}
}
}
class Palindrome{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("enter a number");
int n = in.nextInt();
int a = n;
int palindrome =0;
int r = 0;
while(n>0){
r = n%10;
n = n/10;
palindrome = palindrome*10 + r;
}
System.out.println("Reverse of the entered num is: "+palindrome);
if(palindrome==a){
System.out.println(a+" number is palindrome.");
}else{
System.out.println(a+" number is not palindrome.");
}
}
}
How to swap the two numbers without using 3rd variable.
import java.util.Scanner;
public class Swapping{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("enter 1st number a: ");
int a = in.nextInt();
System.out.println("enter 2nd number b: ");
int b = in.nextInt();
System.out.println("before swapping a="+a+" and b= "+b);
a = a+b;
b = a-b;
a = a-b;
System.out.println("after swapping a="+a+" and b= "+b);
}
}
public class Swapping{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("enter 1st number a: ");
int a = in.nextInt();
System.out.println("enter 2nd number b: ");
int b = in.nextInt();
System.out.println("before swapping a="+a+" and b= "+b);
a = a+b;
b = a-b;
a = a-b;
System.out.println("after swapping a="+a+" and b= "+b);
}
}
Sunday, May 11, 2014
How to login, get the number of mails in inbox and logout from the gmail account.
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class InboxCount {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("enter ur gmail id: ");
String id = kb.nextLine();
System.out.println("enter ur gmail pass: ");
String password = kb.nextLine();
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1<mpl=default<mplcache=2&hl=en&emr=1&elo=1");
driver.findElement(By.id("Email")).sendKeys(id);
driver.findElement(By.xpath("//input[@id='Passwd']")).sendKeys(password);
driver.findElement(By.xpath("//input[@type='checkbox']")).click();
driver.findElement(By.name("signIn")).click();
String inbox = driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/u/0/#inbox']")).getText();
System.out.println(inbox);
//sign out
driver.findElement(By.xpath("//span[@class='gb_V gbii']")).click();
driver.findElement(By.linkText("Sign out")).click();
driver.close();
}
}
How to get the suggestion's text by google which come as dropdown while typing in the search box of google.
package BhanuSir;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class GoogleSearch {
public static void main(String[] args) {
System.out.println("Enter the word which u want to search in Google: ");
Scanner in = new Scanner(System.in);
String srchWord = in.nextLine();
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in/");
driver.findElement(By.name("q")).sendKeys(srchWord);
//get the test suggested by google from the dropdown.
List<WebElement> sugg = driver.findElements(By.xpath("//table[@class='gssb_m']/tbody/tr"));
for(WebElement ele: sugg){
System.out.println(ele.getText());
}
driver.close();
}
}
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class GoogleSearch {
public static void main(String[] args) {
System.out.println("Enter the word which u want to search in Google: ");
Scanner in = new Scanner(System.in);
String srchWord = in.nextLine();
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in/");
driver.findElement(By.name("q")).sendKeys(srchWord);
//get the test suggested by google from the dropdown.
List<WebElement> sugg = driver.findElements(By.xpath("//table[@class='gssb_m']/tbody/tr"));
for(WebElement ele: sugg){
System.out.println(ele.getText());
}
driver.close();
}
}
How to get the current date and time.
import java.util.*;
class GetCurrentDateAndTime
{
public static void main(String args[])
{
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
System.out.println("Current date is "+day+"/"+(month+1)+"/"+year);
System.out.println("Current time is "+hour+" : "+minute+" : "+second);
}
}
write a program to get the Factorial of a number
import java.util.Scanner;
public class Fac{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("enter the num for which u want the factorial");
int num = in.nextInt();
for(int i=num-1; i>0; i-- ){
num = num*i;
}
System.out.println(num);
}
}
Friday, May 9, 2014
Subscribe to:
Posts (Atom)