Skip to main content

Posts

Showing posts with the label selenium examples

Page scroll in selenium with examples

Scroll Down: import org.openqa.selenium.JavascriptExecutor; WebDriver driver = new FirefoxDriver(); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("scroll(0, 250 )"); //y value '250' can be altered Scroll up: JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("scroll( 250 , 0)"); //x value '250' can be altered Scroll bottom of the Page: JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));"); (or) Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform(); Full scroll to bottom in slow motion: for (int second = 0;; second++) {         if(second >=60){             break;         }             ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, 400 )", "&qu

Implicit and Explicit Waits in selenium with examples

Explicit Wait is related with certain conditions to wait; Implicit Wait with specific time to wait for an Element. Explicit Wait Here, the driver waits for 10 secs till the web element is found;  If not, it simply throws a Timeout Exception. WebElement element = (new WebDriverWait(driver, 10 ))   .until(ExpectedConditions. presenceOfElementLocated (By.id(" Value "))); or| WebDriverWait wait = new WebDriverWait(driver, 20 ); WebElement element = wait.until(ExpectedConditions. elementToBeClickable (By.id(" Value "))); WebDriverWait can't be declared globally and throw NullPointerException; whereas the below snippets will help you to do so. // Handles any locator @Test public void Test01() throws Exception {   driver.get(" www.xyz.com ");   wait().until(ExpectedConditions. presenceOfElementLocated (By.xpath(" Value ")));   } private WebDriverWait wait()    {     return new WebDriverWait(driver,  20 );    } // Handl

Page Object in selenium with example

Page Object Pattern is a good approach of implementing Automation tests. It is a language neutral pattern for representing a complete page or portion of a page in an Object Oriented manner. Pattern is a page object, which encapsulates the behavior of the page in an Object oriented manner. It need some programming skills too. Why Page Object? 1| Maintenance 2| Readability of scripts 3| Separation of Concerns Page Object Pattern | Page Factory Lets start with an example using Google.com SCENARIO  1| Open URL 2| Search google 3| Assert page title TEST CLASS | TC.java package  packagename ; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.PageFactory; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TC {  private WebDriver driver;  public ASSERT Task;    @BeforeTest  public void setUp() throw