0% found this document useful (0 votes)
2 views

SelAns

The document provides a comprehensive guide on automating web browsers using Selenium WebDriver, covering various actions such as launching browsers, entering credentials, handling alerts, and performing mouse actions. It includes code snippets for tasks like taking screenshots, managing dropdowns, and implementing waits. Additionally, it lists Selenium interfaces and exceptions, along with methods for handling frames and web tables.

Uploaded by

Sathish Skd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SelAns

The document provides a comprehensive guide on automating web browsers using Selenium WebDriver, covering various actions such as launching browsers, entering credentials, handling alerts, and performing mouse actions. It includes code snippets for tasks like taking screenshots, managing dropdowns, and implementing waits. Additionally, it lists Selenium interfaces and exceptions, along with methods for handling frames and web tables.

Uploaded by

Sathish Skd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

//1,2,3-Browser launch(Chrome,Firefox,IE)

System.setProperty("webdriver.chrome.driver","location");

WebDriver driver=new ChromeDriver();

driver.get("URL");

System.setProperty("webdriver.gecko.driver","location");

WebDriver driver=new FirefoxDriver();

driver.get("URL");

System.setProperty("webdriver.ie.driver","location");

WebDriver driver=new InternetExplorerDriver();

driver.get("URL");

//4.Write a code to automate any webpage(Enter username,password and


Click Login button)
WebElement name = driver.findElement(By.id("email"));

name.sendKeys("abc");

WebElement pass = driver.findElement(By.id("pass"));

pass.sendKeys("123");

WebElement btnLogin = driver.findElement(By.name("login"));

btnLogin.click();

//5.Write a code to get the attribute value & text from Webpage.
WebElement name = driver.findElement(By.id("email"));

name.getAttribute(Value);

String text = name.getText();

//6.Write a code for Mouseover Action.


Actions a=new Actions(driver);

a.moveToElement(findElement).perform();

//7.Write a code for drag and drop.


Actions a=new Actions(driver);

a.dragAndDrop(source, target).perform();
//8.Write a code for Right click.
Actions a=new Actions(driver);

a.contextClick().perform();

//9.Write a code for Double click.


Actions a=new Actions(driver);

a.doubleClick().perform();

//10.Write a code for Keydown & Keyup.


Actions a=new Actions(driver);

a.keyDown(key).perform();

a.keyUp(key).perform();

//11. Write a code for Robot class(ctrl+A,C,V)


Robot r=new Robot();

r.keyPress(KeyEvent.VK_CONTROL_A);r.keyRelease(KeyEvent.VK_CONTROL_A);

r.keyPress(KeyEvent.VK_CONTROL_C);r.keyRelease(KeyEvent.VK_CONTROL_C);

r.keyPress(KeyEvent.VK_CONTROL_V);r.keyRelease(KeyEvent.VK_CONTROL_V);

//12.Write a code for Simple alert,Confirm alert and Prompt alert.


Alert alert = driver.switchTo().alert();

alert.accept();

alert.dismiss();

String text2 = alert.getText();

alert.sendKeys("keysToSend");

alert.accept();

//13.Write a code to handle authentication box/os level/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);

FileUtils.copyFile(screenshotAs, new File("Loction of the file"));


//15.Write a code to take Screenshot for element
WebElement name = driver.findElement(By.id("email"));

File screenshotAs = name.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(screenshotAs, new File("Loction of the file"));

//16.Write a code scroll down/Scroll up.


JavascriptExecutor js=(JavascriptExecutor) driver;

js.executeScript("arguments[0].setAttribute('value','name')", findElement);

js.executeScript("arguments[0].click()", findElement);

js.executeScript("arguments[0].ScrollIntoView(true)", scrollDown );

js.executeScript("arguments[0].ScrollIntoView(false)", scrollUp);

//17.Drop Down -select particular value.


Select s=new Select(findElement);

//18.Drop Down -get the options.


Select s=new Select(findElement);

List<WebElement> options = s.getOptions();

//19.Drop Down -select all values using index.


Select s=new Select(findElement);

int size = options.size();

s.selectByIndex(i);

//20.Drop Down -get the all selected values.


Select s=new Select(findElement);

List<WebElement> allSelectedOptions = s.getAllSelectedOptions();

//s.selectAll();

//s.selectByIndex(index);

//s.selectByVisibleText("text");

//21.Write a code to find frames count.


List<WebElement> findElements = driver.findElements(By.tagName("iframe"));

System.out.println(findElements.size());

22.Write a code to switch to inner frame(Assume we have 3 inner frame).


23.Write a code to switch into frame(possible ways)
By Index

int size = driver.findElements(By.tagName("iframe")).size();

driver.switchTo().frame(0);

driver.switchTo().frame(1);

By Name or Id

driver.switchTo().frame(“iframe1”);

driver.switchTo().frame(“id of the element”);

By Web Element

driver.switchTo().frame(WebElement);

24.Write a code to print images count.


List<WebElement> img = driver.findElements(By.tagName(“a”));

System.out.println(“img count” + img.size());

25.Write a code for windows handling(Assume we have 2 windows)


26.Write any 8 selenium interface.
 SearchContext
 WebDriver
 TakesScreenshot
 JavascriptExecutor
 WebElement
 Alert
 Action
 Options

27.Write any 10 selenium Exceptions.


 NoSuchWindowException
 NoSuchFrameException
 NoSuchElementException
 NoAlertPresentException
 InvalidSelectorException
 TimeoutException
 ElementNotVisibleException
 ElementNotSelectableException
 NoSuchSessionException
 StaleElementReferenceException

28.Write a code for Webtable.


//29.Write a code for implicit wait.
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//30.Write a code for explicit wait.


WebDriverWait wait=new WebDriverWait(elemnt, 30);

wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

//31.Write a code for fluent wait.


FluentWait waits=new FluentWait(driver);

waits.withTimeout(5000,TimeUnit.MILLISECONDS);

waits.pollingEvery(250, TimeUnit.MILLISECONDS);

//32.Write a code to print the links count.


List<WebElement> links = driver.findElements(By.tagName(“a”));

System.out.println(“img count” + links.size());

//33.Write a code to print webtable count.


WebElement roww = driver.findElement(By.tagName("tr"));

roww.getSize();

//34.Write a code to print Dropdown count.


Select s = new Select(driver.findElement(By.id("select drop down locator")));

List<WebElement>l= s.getOptions();

l.size();

//35.Write a code to print Navigation commands.


driver.navigate().to("URL1");

driver.findElement(By.linkText("This is a Link")).click();

driver.navigate().back();

driver.navigate().forward();

driver.navigate().to("URL2");

driver.navigate().refresh();

You might also like