Sunday, January 5, 2020

How to Handle Droup Down in Selenium


Example:- 1

package com.basicselenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class StaticDropdown {

public static void main(String[] args) {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.spicejet.com/");
driver.manage().window().maximize();
Select sel=new Select(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")));
sel.selectByIndex(2);
sel.selectByValue("INR");
sel.selectByVisibleText("AED");

}

}

Example:-2
package com.basicselenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class HelperClass {

public static WebDriver driver;
public static String url="https://www.spicejet.com/";
public static void launchBrowser(){
try{
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
driver=new ChromeDriver();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

public static void maxmizeBrowser(){
try{
driver.manage().window().maximize();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

public static void getUrl(){
try{
driver.get(url);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

public static void clickAdlt(){
driver.findElement(By.id("divpaxinfo")).click();
Select sel_adalt=new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Adult")));
sel_adalt.selectByIndex(5);

Select sel_child=new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Child")));
sel_adalt.selectByValue("3");

Select infant=new Select(driver.findElement(By.id("ctl00_mainContent_ddl_Infant")));
infant.selectByIndex(3);

}



}

---------------
public WebDriver driver;
//public DroupDownupdated(WebDriver driver){
//this.driver=driver;
//}
@Test()
public void tc_01(){
//WebDriver driver = null;
HelperClass.launchBrowser();
//driver=new ChromeDriver();

//System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
//driver.get("https://www.spicejet.com/");
//driver.manage().window().maximize();
HelperClass.getUrl();
HelperClass.maxmizeBrowser();
//driver.findElement(By.id("divpaxinfo")).click();
HelperClass.clickAdlt();

}

}

How to Handle Suggestion Drop Down 
==============================
package com.basicseenum1;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class VerifyDropDown {

public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
driver.findElement(By.id("fromCity")).click();
driver.findElement(By.xpath("//div[@id='react-autowhatever-1']/preceding::input[1]")).sendKeys("Hyd");
Thread.sleep(2000);
driver.findElement(By.xpath("//div[@id='react-autowhatever-1']/preceding::input[1]")).sendKeys(Keys.ARROW_DOWN);
Thread.sleep(2000);
driver.findElement(By.xpath("//div[@id='react-autowhatever-1']/preceding::input[1]")).sendKeys(Keys.ENTER);
Thread.sleep(2000);
//driver.findElement(By.id("toCity")).click();
driver.findElement(By.xpath("//div[@id='react-autowhatever-1']/preceding::input[1]")).sendKeys("PAT");
Thread.sleep(2000);
driver.findElement(By.xpath("//div[@id='react-autowhatever-1']/preceding::input[1]")).sendKeys(Keys.ARROW_DOWN);
Thread.sleep(2000);
driver.findElement(By.xpath("//div[@id='react-autowhatever-1']/preceding::input[1]")).sendKeys(Keys.ENTER);
Thread.sleep(2000);




}

}















Saturday, January 4, 2020

How to upload file in Selenium and How to make dynamic path location

package com.autoit;

import java.io.File;

import java.io.IOException;
import java.util.HashMap;

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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

public class FileUpload {
public static void main(String[] args) throws InterruptedException, IOException {
try{
WebDriver driver;
String downloadPath=System.getProperty("user.dir");//using this steps we will get System path means project path F:\MyWorkSpace\Selenium_Batch_2018
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
//By using below line of code to copy the downlaod file in project locations
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
       chromePrefs.put("download.default_directory",downloadPath);
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
driver=new ChromeDriver(options);
driver.get("https://www.ilovepdf.com/word_to_pdf");
driver.findElement(By.xpath("//span[contains(text(),'Select WORD files')]")).click();
Thread.sleep(3000);
Runtime.getRuntime().exec("D:\\fileupload.exe");
Thread.sleep(3000);
// WebElement ele =driver.findElement(By.id("processTask")).click();
WebElement ele=driver.findElement(By.cssSelector("#processTask"));
Actions act=new Actions(driver);
act.moveToElement(ele).click().build().perform();
Thread.sleep(3000);
//driver.findElement(By.id("pickfiles")).click();
File f=new File(downloadPath+"/Resume.pdf");
if(f.exists()){
System.out.println("File is downloaded");
if(f.delete()){
System.out.println("File is delated");
}
}
else{
System.out.println("File is not downloaded");
}
System.out.println("***Test case is passed***");
}
catch(Exception  e){
System.out.println(e.getMessage());
}
//driver.close();
}
}
=========================================================================




























Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...