http://www.technicalpage.net/search/label/SQL

Important Selenium - Java Codings For Test Automation

Important Java Selenium Codes for Software Test Automation

//--------------------Open Browser(below is an example of Chrome Browser)

System.setProperty("webdriver.chrome.driver","C:\\Path\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

 

//--------------------Maximize window

driver.manage().window().maximize();

 

//--------------------Delete All Cookies

driver.manage().deleteAllCookies();

 

//--------------------Apply implicit wait of 10 seconds

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); 

//Wait for the page load for 10 seconds

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

 

 

//--------------------Open URL(below is an example of google.com)

driver.get("https:\\www.google.com");

driver.navigate().to("https:\\www.google.com");

 

//--------------------Click Button, Enter text on text box, get text of the WebElement

driver.findElement(By.xpath("...")).click();

driver.findElement(By.xpath("...")).sendKeys("abc");

driver.findElement(By.xpath("...")).getText();

 

//--------------------When there are more than one similar webElements

List<WebElement> list = driver.findElements(By.xpath("...")); 

list.size();

list.get(2); // to get the 3rd element

list.get(2).click(); // click 3rd element

 

//--------------------To check if checkbox displayed, selected or enabled

boolean checkBoxDisplayed = driver.findElement(By.xpath("...")).isDisplayed();

boolean checkBoxSelected = driver.findElement(By.xpath("...")).isSelected();

boolean checkBoxEnabled = driver.findElement(By.xpath("...")).isEnabled();

 

//--------------------Get current URL , get title of the page

             driver.getCurrentUrl();

             driver.getTitle();

 

//--------------------navigate forward and backward and refresh

             driver.navigate().forward();

             driver.navigate().back();

             driver.navigate().refresh();

 

//--------------------Get attribute

             driver.findElement(By.xpath("...")).getAttribute("Attribute");

 

//--------------------Get Tagname

             driver.findElement(By.xpath("....")).getTagName();

 

 

//--------------------To handle or switch to windows/Tabs

String tab = driver.getWindowHandle(); //To get an identifier of current window/Tab

Set<String> allTabs = driver.getWindowHandles(); //To get identifiers of all the tabs open at the moment.

 

//To switch to different tabs           

Iterator<String> tabsPresent = allTabs.iterator() ;

String firstTab = tabsPresent.next();

String secondTab = tabsPresent.next();

driver.switchTo().window(secondTab);//To switch to the second tab

 

//--------------------Manage frames

driver.switchTo().frame(driver.findElement(By.xpath("...")));//based on webelement

driver.switchTo().frame(0);//based on index

driver.switchTo().frame("frameName" OR "id");//based on name or id

 

//--------------------To check if alert is present

       public boolean checkAlert() {

             try {

                    driver.switchTo().alert();

                    return true ;

                    } catch(Exception e) {

                           return false ;

                    }

       }

 

//To manage alerts

driver.switchTo().alert().accept();

driver.switchTo().alert().dismiss();

String alertText = driver.switchTo().alert().getText();

 

//--------------------Use Keyboard

             driver.findElement(By.xpath("....")).sendKeys(Keys.ENTER);

             driver.findElement(By.xpath("....")).sendKeys(Keys.TAB);

             driver.findElement(By.xpath("....")).sendKeys(Keys.SPACE);

 

//--------------------Java Script Executor

JavascriptExecutor js = (JavascriptExecutor)driver ;

WebElement buttonToClick = driver.findElement(By.xpath("..."));

js.executeScript("arguments[0].click()", buttonToClick); //Click 

OR

js.executeScript("arguments[0].click()", driver.findElement(By.xpath("...")));//Click

 

js.executeScript("arguments[0].value='Text to Print'", driver.findElement(By.xpath("...")));//SendKeys

//Java Script Executor SCROLL

js.executeScript("window.scrollBy(0,300)",""); //or

js.executeScript("window.scrollBy(0,300)");// scroll vertically by 300 px

 

//--------------------Mouse hoVer , drag and drop

Actions action = new Actions(driver); //import org.openqa.selenium.interactions.Actions;

action.moveToElement(driver.findElement(By.xpath("..."))).build().perform(); //mourse hover

action.dragAndDrop(driver.findElement(By.xpath("...")), driver.findElement(By.xpath("...")));//drag and drop, the first webelement in the bracket is source element, the second webelement is target element

 

//--------------------Drop down

Select select = new Select(driver.findElement(By.xpath("....")));

select.selectByIndex(0);

select.selectByValue("value");

select.selectByVisibleText("visible text");

List<WebElement> dropDownItems = select.getOptions();// To get all the drop down items

 

//--------------------Coordinates

driver.manage().window().getPosition().getX();

driver.manage().window().getPosition().getY();

Point coordinates = driver.manage().window().getPosition();//To get both coordinates

coordinates.getX();

coordinates.getY();

 

//--------------------Close Browser

driver.close(); //Closes current browser

driver.quit(); //Closes entire session

 

//--------------------Array List -----------------

             ArrayList<Integer> alist = new ArrayList<>();

             ArrayList<String> blist = new ArrayList<>();

             alist.add(5);

             blist.add("K");

             alist.add(6);

             blist.add("M");

             alist.size();

             alist.get(0);

             blist.get(0);

             System.out.println(alist.size());//2

             System.out.println(alist);//[5, 6]

             System.out.println(blist);//[K, M]

             System.out.println(alist.get(0));//5

             System.out.println(blist.get(0));//K

             alist.indexOf(6);

             blist.indexOf("K");

             System.out.println(alist.indexOf(6));//1

             System.out.println(blist.indexOf("K"));//0

 ///-----------------ArrayList Remove

             ArrayList<String> alist = new ArrayList<>();

             alist.add(0,"A");//add at index zero

             alist.add("StringATindex1");

             alist.add("B");

             alist.add("C");

             System.out.println("alist before remove "+alist);

             alist.remove(0);

             System.out.println("alist after remove "+alist);

             alist.remove(1);

             System.out.println("alist after 2nd remove "+alist);

            

Output:

alist before remove [A, StringATindex1, B, C]

alist after remove [StringATindex1, B, C]

alist after 2nd remove [StringATindex1, C]

 

 

//--------------------ArrayList and Array display same output format

System.out.println(arrayList); == System.out.println(Arrays.toString(array));

For example:

ArrayList<Integer> alist = new ArrayList<>();

int[] array = new int[2];

alist.add(3) ;

alist.add(4) ;

array[0] = 3;

array[1] = 4; 

System.out.println(alist); //Output is [3,4]

System.out.println(Arrays.toString(array)); //Output is [3,4]

 

//--------------------ArrayList without datatype

             ArrayList alist = new ArrayList<>();

             alist.add("Japan");

             alist.add(3.14);

             alist.add(5);

             System.out.println("ArrayList Values are : "+alist);

Output:

ArrayList Values are : [Japan, 3.14, 5]

 

 

//--------------------Array-------------

//Different ways to write an array

             String[] findIndex1 = new String[4];

            OR String[] findIndex2 = new String[] {"","","","",""};

            OR String[] findIndex = {"b","e","a","u","t","i","f","u","l","u","n","i","v","e","r","s","e"};

System.out.println("index of s : "+Arrays.toString(findIndex).indexOf("b"));//1 wrong

System.out.println("index of s : "+Arrays.asList(findIndex).indexOf("b"));//0 correct

System.out.println("index of d : "+Arrays.toString(findIndex).indexOf("u"));//10 wrong

System.out.println("index of d : "+Arrays.asList(findIndex).indexOf("u"));//3 correct

 

 

             int[] array = new int[]{2,1,5,2,6,3,7};  //there is no size

             String[] arrayOne = new String[10];              //There is size

             arrayOne[0] = "M";

             arrayOne[1] = "T";

             arrayOne[2] = "4";

             System.out.println(array.length);//7

             System.out.println(Arrays.toString(array).indexOf(6));// 4

             System.out.println(Arrays.asList(array).indexOf(6));// 4

             System.out.println(array[2]);//5

             System.out.println(arrayOne[2]);//4

             System.out.println(Arrays.asList(array[2]));//[5]

             System.out.println(Arrays.asList(arrayOne[2]));//[4]

             System.out.println(array);//[I@4926097b

             System.out.println(arrayOne);//[Ljava.lang.String;@762efe5d

             System.out.println(Arrays.asList(array));//[[I@4926097b]

             System.out.println(Arrays.asList(arrayOne));//[M, T, 4, null, null, null, null, null, null, null]

             System.out.println(Arrays.toString(array));//[2, 1, 5, 2, 6, 3, 7]

             System.out.println(Arrays.toString(arrayOne));//[M, T, 4, null, null, null, null, null, null, null]

 

//-----------you can sort Array, you can copy both array and arraylist

//-----------Sort and Copy Array

       public static void main(String[] args) {

             int[] num = {1,8,4,0,4,7,9};

             int[] copiedArray= num; //copied

             System.out.println("copiedArray= "+ Arrays.toString(copiedArray));

             //int[] num2 = Arrays.sort(num); //this does not work

             Arrays.sort(num);

             System.out.println("sorted num = "+Arrays.toString(num)); 

            

             String[] str = {"Gopi","Anil","Rita","Victer","Boby"};

             Arrays.sort(str);

             System.out.println("sorted string = "+Arrays.toString(str));      

       }

Output:

copiedArray= [1, 8, 4, 0, 4, 7, 9]

sorted num = [0, 1, 4, 4, 7, 8, 9]

sorted string = [Anil, Boby, Gopi, Rita, Victer]

 

 //-----------Copy ArrayList

       public static void main(String[] args) {

             ArrayList<String> alist = new ArrayList<>();

             alist.add("A");

             int lengthAlist = alist.size();

             alist.add("5");

             alist.add("3.14");

            

             ArrayList<String> blist = alist;

             System.out.println("blist = "+blist);

//OR//

ArrayList<String> blist = new ArrayList<String>();

blist = alist; 

System.out.println("blist = "+blist);

       }

Output:

blist = [A, 5, 3.14]

 

//----------Return Array and Arraylist 

       public static ArrayList<String> ArrayListTest(ArrayList<String> alist) {

             ArrayList<String> blist = new ArrayList<>();

             blist = alist;

             return blist;

 

       }

      

       public static int[] ArrayTest(int[] num) {

             int[] num1 = num;

             return num1;

 

       }

      

       public static void main(String[] args) {

             ArrayList<String> clist = new ArrayList<String>();

             clist.add("M");

             clist.add("G");

             clist.add("H");

            

             ArrayList<String> dlist = ArrayListTest(clist);

            

             int[] empID = {5,7,1,0};

             int[] empNum = ArrayTest(empID);

            

             System.out.println("ArrayList dlist = "+dlist);

             System.out.println("Array EMPNUM = "+Arrays.toString(empNum));

       }

 

Output:

ArrayList dlist = [M, G, H]

Array EMPNUM = [5, 7, 1, 0]

 

//--------------------Array with word/string

             String words = "umbrella55s";

             System.out.println(words.length());//11

             System.out.println(words.charAt(4));//e

             System.out.println(Character.toString(words.charAt(4)));//e

       System.out.println(Integer.parseInt(Character.toString(words.charAt(9))));//5

             System.out.println(words.indexOf("a"));//7

             System.out.println(words.lastIndexOf("5"));//9

1 comment:

  1. What Does the RTP Rate in the roulette table - BetsJeon
    This is a high 바카라에볼루션 variance table 인터넷바카라 with a wheel 드래곤 타이거 spinning 먹튀신고 in roulette, while the maximum bet is a minimum bet. A 벳 365 가상 축구 주소 single bet has two bets. The

    ReplyDelete