Posts

To check if Web Element is present

To verify if an element is present in the screen  and based on the condition to proceed, then we can check the size of element and if it is greater than zero then the element exists or else element does not exists Ex: Consider login screen where need to check if password field exists or not let password is represented by id as pswrd use the below command List<WebElement> pwd = driver.findElements(By.id("pswrd")); if(pwd.size()>0{ // password field exists; }else{  // password field does not exists; }

Moving between frames...

Consider an application where the contents are in  different frames  and need to fetch data from a frame FRAME-1 FRAME-2 FRAME-3 In the above example consider that after selecting an item in frame 2 the contents are displayed in frame 3 now need to read the data in frame 3 public void default_frame(){ try{ driver.switchTo().defaultContent(); frames(Frame1,fmset); }catch(Exception e){ e.printStackTrace(); } } In the above code first we are moving to default frame by switching to default content public void frames(final String x, final String y){ try{ driver.switchTo().frame(x); System.out.println(":::::::::: Frame 1 is switched " + x); driver.findElement(By.tagName(y)); System.out.println(":::::::::: Frameset value " + driver.findElement(By.tagName(y)) + "::::"); }catch(Exception e){ e.printStackTrace(); } } In the above code after moving to frames, now need to go t...

Changing driver (i.e. from Chrome driver to firefox driver) from properties file

Create a property file (property) In the property file enter Webdrivername = org.openqa.selenium.chrome.ChromeDriver Next time if need to run script in say IE, Firefox, Opera just change the content in property file. Below is the Java code which can be used... public WebDriver driver  = browserDriver(); public static WebDriver drv  = null; public static WebDriver browserDriver() { File file1 = new File(/*The path for property the driver of that browser */); if(drv==null){ try{ switch (sdriver) { case "chrome": File FileChrome  = new File(file1+"/chromedriver.exe"); System.setProperty("webdriver.chrome.driver", FileChrome.getAbsolutePath()); browserName("org.openqa.selenium.chrome.ChromeDriver"); break; case "ie": File fileIE = new File(file1+"/IEDriverServer.exe"); System.setProperty("webdriver.ie.driver", fileIE.ge...

Clicking a web element on mouse-over

Below is the sample code which helps for mouseover in selenium webdriver public void mouseover(String x){      WebElement wel = driver.findElement(By.id(x)); System.out.println(":::::::::: String the id is :"+x); Actions builder = new Actions(driver); builder.moveToElement(wel).click(wel); builder.perform(); }

Moving between Windows in selenium

Upon clicking let us assume that a new window page is displayed public void stringhandles(){ Set<String>handles = driver.getWindowHandles(); System.out.println("popopopo"+handles.size()); System.out.println("popopopo"+handles); for(String handle:handles){ for(i=0;i<handles.size();i++){ System.out.println( i); if(i==0){ continue; } } driver.switchTo().window(handle); } }

getting future date

Date part ()  public static Calendar cldr; public static String f_date; public static  Calendar d = Calendar.getInstance(); public static SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy"); public void datepart(){ try { cldr = (Calendar) d.clone(); if(cldr.get(Calendar.DAY_OF_WEEK)<=5){ cldr.add(Calendar.DAY_OF_WEEK,+1); f_date = sdf1.format(cldr.getTime()); System.out.println("PPPPPPPPPP "+ f_date); } else if(cldr.get(Calendar.DAY_OF_WEEK)==6){ cldr.add(Calendar.DAY_OF_WEEK,+3); f_date = sdf1.format(cldr.getTime()); System.out.println("QQQQQQQQQQQ "+ f_date); } else if(cldr.get(Calendar.DAY_OF_WEEK)==7){ cldr.add(Calendar.DAY_OF_WEEK,+2); f_date = sdf1.format(cldr.getTime()); System.out.println("RRRRRRRRR "+ f_date); } else if(cldr.get(Calendar.DAY_OF_WEEK)==0){ cldr.add(Calendar.DAY_OF_WEEK,+1); f_date = sdf1.format(cldr.getT...

How to capture screen while running test suite

Create a file path file1 location where  the image needs to be stored SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ssaa") Date date=new Date(); public void screenshot() { try { System.out.println("Capturing screen........."); String filepath = file1+"\\image"+sdf.format(date)+new Date().getTime()+".png"; File srcfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); System.out.println("capturing.............."); FileUtils.copyFile(srcfile, new File(filepath)); Count++; String link1 = "<a href = "+filepath+">screenshot"+Count+"</a>"; org.testng.Reporter.log(link1); System.out.println("Image Captured"); } catch (IOException e) { e.printStackTrace(); } } In the above code the Just a System.out.println in the console location where the captured file is stored  This is a selenium command, where t...