Wednesday, 15 June 2016

How to check whether the given number is prime number or not


public class PrimeNumber_Or_Not {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
int x;
{
    for(x=2;x<=10;x++)
        if(x%2==0)
        {
            System.out.println("not prime" + x);
           
        }
    else {
        System.out.println("prime " + x);
    }
}
    }

   
   
}
Output:
not prime2
prime 3
not prime4
prime 5
not prime6
prime 7
not prime8
prime 9
not prime10

fibonacci example program


public class Fibonacy {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
int a=0;
int b=1;
for(int i=0;i<10;i++)
{
   
    a=a+b;
    //System.out.println("a data"+a);
    b=a-b;
    System.out.println("b data "+b);
}
    }

}

Output:
b data 0
b data 1
b data 1
b data 2
b data 3
b data 5
b data 8
b data 13
b data 21
b data 34

Handling popups using AutoIt


Tuesday, 7 June 2016

How to compare string with and withod using equals

+++String comparison with using equals ++++
package java_interview_logics_practice;

public class CompareStringWithEqual {

    public static void main(String[] args) {
        String s1="abcd";
        String s2="abcd";
        if(s1.equals(s2))
        {
            System.out.println("s1 is equals with s2 :");
        }
        else
        {
            System.out.println("s1 is not equals with s2 :");
        }

    }

}

+++++++++String comparison without using Equals  ++++++

package java_interview_logics_practice;

public class CompareStringWithoutEquals {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
String s1="abcd";
String s2="abcd";
if(s1.length()==s2.length())
{
    for(int i=0;i<s1.length();i++)
    {
        if(s1.charAt(i)!=s2.charAt(i))
        {
            System.out.println("String "+s1+" is not equal to string "+s2);
            break;
        }
    }
     System.out.println("String "+s1+" is equal to string "+s2);
}
else
{
   
    System.out.println("String "+s1+" is not equal to string "+s2);
}

}


    }




How to print values (drop down) using mouse over in selenium(nop ecommerce application)

package pageFactory_POM;

import java.util.ArrayList;
import java.util.List;
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.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class Example {
    static WebDriver driver;
    //WebDriver driver=new FirefoxDriver();
   
    public List<WebElement> elements;
    @Test
    public void verifyDashboradTabOnAdminpage() throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver","D:\\selenium_NEW_PRACTICE\\softwares\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();       
       
        driver.get("http://admin-demo.nopcommerce.com/admin/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("admin@yourstore.com");
        driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("admin");
        driver.findElement(By.xpath("//input[@class='button-1 login-button']")).click();
        Thread.sleep(2000);
       
        //click on catalog button
        WebElement ele=driver.findElement(By.xpath("//img[@src='/Administration/Content/images/ico-catalog.png']"));
        ele.click();
        Thread.sleep(2000);
        System.out.println("Catalog is clicked");
        int catalogMenuSize = driver.findElements(By.xpath("//ul[@id='admin-menu']/li[2]/div/ul/li")).size();
        System.out.println("totall option from dd:" + catalogMenuSize);
        Actions action = new Actions(driver);
        Actions dd = new Actions(driver);
       
        for(int i=1;i<=catalogMenuSize;i++){
            if (i!=2){
                action.moveToElement(driver.findElement(By.xpath("//ul[@id='admin-menu']/li[2]/span"))).build().perform();
                dd.moveToElement(driver.findElement(By.xpath("//ul[@id='admin-menu']/li[2]/div/ul/li[" + i + "]/span"))).build().perform();
                System.out.println( i + ":: " + driver.findElement(By.xpath("//ul[@id='admin-menu']/li[2]/div/ul/li[" + i + "]/span")).getText());
                List<WebElement> elem2 = driver.findElements(By.xpath("//ul[@id='admin-menu']/li[2]/div/ul/li["+i+"]/div/ul/li/a"));
                for(int j=1;j<=elem2.size();j++){
                String subxpath ="(//ul[@id='admin-menu']/li[2]/div/ul/li["+i+"]/div/ul/li/a)["+j+"]";
                System.out.println(" :::: " + driver.findElement(By.xpath(subxpath)).getText());
                }
                }
                else {
                action.moveToElement(driver.findElement(By.xpath("//ul[@id='admin-menu']/li[2]/span"))).build().perform();
                dd.moveToElement(driver.findElement(By.xpath("//ul[@id='admin-menu']/li[2]/div/ul/li[" + i + "]/a"))).build().perform();
                System.out.println( i + ":: " + driver.findElement(By.xpath("//ul[@id='admin-menu']/li[2]/div/ul/li[" + i + "]/a")).getText());
                }
           
            }
    }
}
       
   
       
       
       
       
       
       

Friday, 3 June 2016

How to handle JavaScript alerts or window based popups using Robot keys and selenium

package rough;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.chainsaw.Main;
import org.openqa.selenium.Alert;
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.mysql.jdbc.Driver;

public class WindowAlertPopup_Handling {
static WebDriver driver;
public static void main(String[] args) throws AWTException, InterruptedException {
    int n = 9;
    //driver=new FirefoxDriver();
    System.setProperty("webdriver.chrome.driver","D:\\selenium_NEW_PRACTICE\\softwares\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("http://www.jtable.org/");   
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//*[contains(text(),'Export to Excel')]")).click();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    Thread.sleep(1000);
    /*
    //working fine using Robot keys
    Robot robot=new Robot();
    robot.keyPress(KeyEvent.VK_ALT);
     robot.keyPress(KeyEvent.VK_S);
     robot.keyRelease(KeyEvent.VK_S);
     robot.keyRelease(KeyEvent.VK_ALT);
     robot.keyPress(KeyEvent.VK_ENTER);
      System.out.println("File is downloaded");
      */
   
    //working fine using selenium
    Alert al=driver.switchTo().alert();
    String aleartText=al.getText();
    al.accept();
    System.out.println("clicking alert popup");
   

}
}

How to perform actions on table by using perticular text

package rough;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.chainsaw.Main;
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.mysql.jdbc.Driver;

public class If_PerticularTextContains_PerformActions {
static WebDriver driver;
public static void main(String[] args) {
    int n = 9;
    //driver=new FirefoxDriver();
    System.setProperty("webdriver.chrome.driver","D:\\selenium_NEW_PRACTICE\\softwares\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("http://www.jtable.org/");   
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    WebElement table=driver.findElement(By.xpath("//*[@id='StudentTableContainer']/div/table"));
    List<WebElement> th=driver.findElements(By.tagName("th"));
    int col_position=0;
    for(int i=0;i<th.size();i++)
    {
    System.out.println(th.get(i).getText());
     if("City".equalsIgnoreCase(th.get(i).getText())){
         col_position=i+1;
         break;
     }
    }
     List<WebElement> FirstColumns = table.findElements(By.xpath("//tr/td["+col_position+"]"));
    String firstNameOnTable=FirstColumns.get(0).getText();
     System.out.println("first :" + firstNameOnTable);
        for(WebElement e: FirstColumns){
         String ss = e.getText();
            System.out.println(ss);
           
            if(ss.equalsIgnoreCase(firstNameOnTable))
                System.out.println("one");
            {
                    driver.findElement(By.xpath("//*[@id='StudentTableContainer']/div/table/tbody/tr[1]/td[9]/button")).click();
           }    
          
    }
   
   
driver.close();
   
}
}