After locating elements, now we will use Selenium command to interact with them. Interaction including click, input text, get text, ect. Below are all basic commands you need to learn before go to testcase creation:
Open Browser:
- FireFox Driver
driver = new FirefoxDriver();
- Chrome Driver – You have to download Chrome Driver here and save it into your hard disk (ex: D:\ChromeDriver)
String chromePath ="D:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromePath );
driver = new ChromeDriver();
- Internet Explorer Driver – You have to download IE Driver here and save it into your hard disk (ex: D:\IEDriverServer)
String IEPath="D:\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", IEPath);
DesiredCapabilities capab = DesiredCapabilities.internetExplorer();
capab.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
driver = new InternetExplorerDriver(capab);
Close Browser:
driver.close(); //or driver.quit();
Maximize Browser Windows:
driver.manage().window().maximize();
Open Url:
driver.get("google.com.vn");//Go to google.com.vn
Click on Element:
driver.findElement(By.cssSelector("button[type='submit']")).click();
Store text of targeted element in variable:
String text = driver.findElement(By.id("selenium")).getText();
Input text in text box or text area:
driver.findElement(By.id("textarea")).sendKeys("Selenium");
Get Page Title:
String title = driver.getTitle();//Store to variable
Get Current URL:
String url = driver.getCurrentUrl();//Store to variable
Navigation: Back, Foward and Refresh:
driver.navigate().back(); driver.navigate().forward(); driver.navigate().refresh();
Handling Alert, Confirmation and Prompt popup:
Generally alert message popup is displayed with alert text and Ok button as shown in bellow given Image:
And here is Confirmation Pop-up
Prompt Pop-up:
We can interact with them by using following commands:
//Accept
driver.switchTo().alert().accept();
//Discard
driver.switchTo().alert().dismiss();
//Get Text of alert/confirmation pop-up
Alert A1 = driver.switchTo().alert();
String text = A1.getText();
//Input Text into Prompt Pop-up
Alert A2 = driver.switchTo().alert();
String text = A2.sendKeys("Thieu Nguyen");
A2.accept();
Handling multiple browser windows:
Suppose that you click on a button, and it opens a new page/tab. The following script will help you to choose what browser window you want to interact with:
String window1 = driver.getWindowHandle();//Get window 1 handle
//Click on submit button and it will open in new window (window 2)
driver.findElement(By.cssSelector("h4.submit")).click();
String window2 = driver.getWindowHandle();//Get window 2 handle
/*Do something in window 2
*
*/
//Switch to window1
driver.switchTo().window(window1);
/*Do something in window 1
*
*/
//Switch again to window 2
driver.switchTo().window(window2);
Wait:
Wait for several seconds:
//wait for 15 seconds driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Wait for element is clickable:
WebDriverWait wait = new WebDriverWait(driver, 15);//Time out is 15 seconds
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
Wait for alert present:
WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.alertIsPresent());
Wait for alert present:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='selenium']"), "Selenium"));
Wait for element visible/invisible:
WebDriverWait wait = new WebDriverWait(driver, 15);
//wait for element visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='WebDriver']")));
//wait for element invisible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='TestNG']")));
tobe continue…























