1.Browser Launch and Print title in chrome Browser?
System.setProperty("webdriver.chrome.driver", "Driver Path");
WebDriver driver = new ChromeDriver();
String title = driver.getTitle()
System.out.println(title);
2.Browser Launch and Print title in IE Browser?
System.setProperty("webdriver.ie.driver", "Driver Path");
WebDriver driver = new InternetExplorerDriver();
String title = driver.getTitle()
System.out.println(title);
3.Browser Launch and Print title in FireFox Browser?
System.setProperty("webdriver.gekco.driver", "Driver Path");
WebDriver driver = new FirefoxDriver();
String title = driver.getTitle()
System.out.println(title);
4. Write a code to automate any webpage (Enter Username , password and
click login button) ?
System.setProperty("webdriver.chrome.driver"," Driver Path ");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement email = driver.findElement(By.xpath("//input[@id='email']"));
email.sendKeys("vinoth");
WebElement pass = driver.findElement(By.xpath("//input[@id='pass']"));
pass.sendKeys("12345678");
WebElement login = driver.findElement(By.xpath("//button[@name='login']"));
login.click();
5. write a code to get attribute value and text from webpage?
//Get Attribute
String attribute = webelementRef.getAttribute("value");
System.out.println(attribute);
//Get Text
String text = webelementRef.getText();
System.out.println(text);
6. write code for mouseover action?
Actions a = new Actions(driver);
a.moveToElement(ref).perform();
7. write code for Drag and Drop?
Actions a = new Actions(driver);
a.dragAndDrop(source, target).perform();
8. write code for right click?
Actions a = new Actions(driver);
a.contextClick(ref).perform();
9. write code for double click?
Actions a = new Actions(driver);
a.doubleClick(ref).perform();
10. write code for keyDown and KeyUp?
Actions a = new Actions(driver);
a.keyDown(Keys.SHIFT).perform();
ref.sendKeys("vinoth");
a.keyUp(Keys.SHIFT).perform();
O/P : VINOTH
11.Write a code for Robot class(C+A, C+C, C+V)
Robot r = new Robot();
(C+A)
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_CONTROL);
(C+C)
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_C);
r.keyRelease(KeyEvent.VK_C);
r.keyRelease(KeyEvent.VK_CONTROL);
(C+V)
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
12 Write a code for simple alert, confirm alert and prompt alert
Alert alert = driver.switchTo().alert();
simple alert :
alert.accept();
confirm alert :
alert.accept();
alert.dismiss();
prompt alert :
alert.accept();
alert.dismiss();
alert.sendKeys(value);
13.Write a code to handle authentication box/os leyel/Windows based
popups
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(username, password));
14,Write a code to take Screenshot
TakesScreenshot ts=(TakesScreenshot) driver;
File screenshotAs = ts.getScreenshotAs(OutputType.FILE);
File image = new File("Loction of the file");
FileUtils.copyFile(screenshotAs,image);
15.Write a code to take Screenshot for Element
TakesScreenshot ts=(TakesScreenshot) driver;
File screenshotAs = webelementRef.getScreenshotAs(OutputType.FILE);
File image = new File("Loction of the file");
FileUtils.copyFile(screenshotAs,image);
16.write a code insert, click using js
JavascriptExecutor js =(JavascriptExecutor)driver;
js.executeScript("arguments[0].setAttribute('value','java’)", webelementRef);
js.executeScript("arguments[0].click()", webelementRef);
17.write a code Scroll down/scroll up
JavascriptExecutor js =(JavascriptExecutor)driver;
js.executeScript("arguments[0].ScrollIntoView(true)", webelementRef );
js.executeScript("arguments[0].ScrollIntoView(false)", webelementRef);
18.Drop Down-select particular value
Select s=new Select(webelementRef);
s.selectByValue(value);
19.Drop Down-get the options
Select s=new Select(webelementRef);
List<WebElement> options = s.getOptions();
20. Drop Down-select all values using index
Select s=new Select(webelementRef);
List<WebElement> options = s.getOptions();
for (int i=0; i < options.size(); i++) {
s.selectByIndex(i);
}
21. Drop Down-get the all selected values
Select s=new Select(webelementRef);
List<WebElement> aso = s.getAllSelectedOptions();
System.out.println(aso);
22.Drop Down-deselect all values by using index
Select s=new Select(webelementRef);
s.deselectByIndex(i);
23.Drop Down-deselect all values by using deselectAll()
Select s=new Select(webelementRef);
s.deselectAll();
24. Drop Down- Write a code to get the all options count
Select s=new Select(webelementRef);
List<WebElement> options = s.getOptions();
Int count = options.size();
System.out.println(count);
25.Write a code to find frames count
List<WebElement> frames = driver.findElements(By.tagName("iframe"));
Int count = frames.size();
System.out.println(count);
26,Write a code to switch to inner frame (Assume we have 3 inner frame)
driver.switchTo().frame(0);
driver.switchTo().frame(1);
driver.switchTo().frame(2);
27 Write a code to switch into frame(possible ways)
By Index
driver.switchTo().frame(index);
By Name or Id
driver.switchTo().frame(“id or name”);
By Web Element
driver.switchTo().frame(WebElement);
28.Write a code to print images count
List<WebElement> img = driver.findElements(By.tagName(“img”));
Int count = img.size();
System.out.println(count);
29.Write a code for windows handling (Assume we have 2 windows)
String mainwindow = driver.getWindowHandle();
Set<String> child = driver.getWindowHandles();
Iterator<String> il = child.iterator();
while(il.hasNext()) {
String nextwindow = il.next();
if (!nextwindow.equals(mainwindow)) {
driver.switchTo().window(nextwindow);
30.Write any 8 selenium interfaces
WebDriver
TakesScreenshot
JavascriptExecutor
WebElement
Alert
Navigation
SearchContext
OutputType
31.Write any 10 Selenium Exceptions
NoSuchWindowException
NoSuchFrameException
NoSuchElementException
NoAlertPresentException
InvalidSelectorException
TimeoutException
ElementNotVisibleException
ElementNotSelectableException
NoSuchSessionException
StaleElementReferenceException
32.Write a code for Webtable
WebElement table = driver.findElement(By.xpath("table_xpath"));
List<WebElement> theading = table.findElements(By.tagName("th"));
List<WebElement> trow = table.findElements(By.tagName("tr"));
List<WebElement> tdata = table.findElements(By.tagName("td"));
33. expWrite a code for implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(timevalue));
34 Write a code for explicit wait
WebDriverWait wait=new WebDriverWait(driver, Duration.ofSeconds(timevalue));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
35.Write a code for fluent wait
Wait<WebDriver> wait = newFluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(sec))
.pollingEvery(Duration.ofSeconds(second)).ignoring(Exception.class);
wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.xpath(value));
}
});
36.Write a code to print the links count
List<WebElement> links = driver.findElements(By.tagName(“a”));
Int count = links.size();
System.out.println(count);
37.Write a code to print Webtable's count
List<WebElement> table = driver.findElements(By.tagname("table"));
Int count = table.size();
System.out.println(count);
38.Write a code to print Dropdown's count
List<WebElement> dropdownboxs = driver.findElements(By.tagname("select"));
Int count = dropdownboxs.size();
System.out.println(count);
39.Write a code for all Navigation commands
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.navigate().to(“url”);