Published March 12, 2018 by

Nth Even Fibonacci Number

Nth Even Fibonacci Number

I this post, we'll learn about how to calculate Nth Even Fibonacci Number.

The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones.


In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation,

 Fn = Fn-1 + Fn-2  with seed values

 F0 = 0 and F1 = 1.

The even number Fibonacci sequence is : 0, 2, 8, 34, 144, 610, 2584…. We have to find nth number in this sequence.

The formula of even Fibonacci number =  ((4*evenFib(n-1)) + evenFib(n-2));


The Below Code shows how to calculate Nth Even Fibonacci Number in Java. 























 public class NthEvenFibonacciNumber {
public static void main(String[] args) {
    // Here we are passing nth Number
int nthNumber = 3;
//Calling the getEvenNumber Method And Printing the Nth Even Fibonacci Number number
System.out.println(getEvenNumber(nthNumber));
}
public static int getEvenNumber(int nthNumber) {
if(nthNumber < 1) {
return nthNumber;
}
else if(nthNumber == 1) {
return 2;
}
else {
//Formula to calculate Nth Even Fibonacci Number  Fn = 4*(Fn-1) + Fn-2
return ((4 * getEvenNumber(nthNumber - 1)) + getEvenNumber(nthNumber - 2));
}
}

}

            Output :- 34 



If You like my post please like & Follow the blog, And get upcoming interesting topics.
Read More
Published March 11, 2018 by

How to handle Lazy Loading Webpage

How to handle Lazy Loading Webpage

In this post, we'll learn about lazy Loading & How to handle Lazy Loading.


What is Lazy Loading?

There is a new technology in the web development, which is called Lazy Loading. This makes your web page lighter and load fast. 

How it works is that it loads the page only what is visible on the screen, and on scrolling down the page, it agains loads the rest of the page. So it loads only the visible part of the webpage, not the whole web page as a whole. 

How to handle Lazy Loading?

So solution to your problem is to scroll down the web page where your element is visible. Make sure you do not scroll it down completely at a go, as if you directly reach to the bottom of the page, then again it will not load the middle web page. 

So your interested element will be active only when you load that part of the page.

Here to scroll the page we are using Java- Script

The Syntax is as follows:-

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

The Below Code shows the lazy loading handling on Pinterest. 

The Source Code for this topic is available on GitHub Repository, You can get it from this URL:-




































package com.lazyloading.ui.test;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class LazyLoading {

WebDriver driver;
int scrollLimit = 10;
// Note:- Use your Email-Id & Password below to run this script
String emailId = "emailId";
String password = "password";
@BeforeMethod
public void setup() {
//Launch browser and go to a URL
System.setProperty("webdriver.chrome.driver", "/Users/ShubhamPatil/Desktop/chromedriver");
driver = new ChromeDriver();
driver.get("https://www.pinterest.com");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

@Test
public void test() throws InterruptedException {
//Login to Pinterest site
driver.findElement(By.id("email")).sendKeys(emailId);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.xpath("//div[text()='Continue']")).click();

//Verify Home page
Assert.assertTrue(driver.findElement(By.xpath("//div[text()='Home']")).isDisplayed(), "Home Page is not displayed");

// Call to scroll Method
scrollDown();
}

// Scroll Method, Scroll the page using Java Script
public void scrollDown() throws InterruptedException {
for (int i = scrollLimit; i >= 0; i--) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
// Wait to load the scrolled page
Thread.sleep(1000);
}
}


}
The Source Code for this topic is available on GitHub Repository, You can get it from this URL:-


If You like my post please like & Follow the blog, And get upcoming interesting topics.
Read More
Published March 10, 2018 by

ListBox Handling Techniques


ListBox Handling Techniques

In this post, we'll learn how to handle listbox/DropDown list.

To handle the listbox, we use Select class of selenium. It should be imported from the following packages: import org.openqa.selenium.suport.ui.Select
Select class has parameterized constructor (single arg constructor) it takes an argument type WebElement (address of the listbox). In order to select the required option present in the listbox we can
use any one the following method of Select class.
  1. selectByVisibleText(str) > takes string argument
  2. selectByIndex(int) > takes integer argument
  3. selectByValue(str) > takes string argument
If the specified option is duplicate in will select first matching option(in dropdown list) and if the specified option is not present(text, value or index), we get NoSuchElementException.
Select class can also be used to handle mulitselect listbox. If the specified option is duplicate in mutliselect listbox, it selects all the matching option.
In Select class we also have the following 4 methods. This can be used on Multiselect listbox


  1. deselectByVisibleText(str) 
  2. deselectByIndex(int)
  3. deselectByValue(str)
  4. deselectAll()
Type Of listbox:-

Single Select Listbox:-
The below is the sample html code,


<html>
         <body>
                     <Select name=“Country”>
                     <option value=“USD”>United States</options>
                     <option value=“GBU”>United Kingdom</options>
                     <option value=“CAD”>Canada</options>
                     <option value=“BZL”>Brazil</options>
                     <option value=“AUD”>Australia</option>
                     </Select>
         </body>

  </html>




Multi-Select Listbox:-
The below is the sample html code,

<html>
         <body>
                     <Select name=“Country” multiple size = "10">
                     <option value=“USD”>United States</options>
                     <option value=“GBU”>United Kingdom</options>
                     <option value=“CAD”>Canada</options>
                     <option value=“BZL”>Brazil</options>
                     <option value=“AUD”>Australia</option>
                     </Select>
         </body>
  </html>




The syntax for select class is as follows:-

Select select new Select(WebElement);

select.selectByVisibleText(StringArgument); 

Method Name :- selectByVisibleText

Syntax:- select.selectByVisibleText("Text");
Purpose: It is very easy to choose or select an option given under any dropdown and multiple selection boxes with selectByVisibleText method. It takes a parameter of String which is one of the Value of Select element and it returns nothing.
Select select = new Select(driver.findElement(By.name("Country")));
select.selectByVisibleText("Canada");


Method Name :- selectByIndex

Syntax:- select.selectByIndex(index);
Purpose:- It is almost the same as selectByVisibleText but the only difference here is that we provide the index number of the option here rather the option text.It takes a parameter of int which is the index value of Select element and it returns nothing.


Select select = new Select(driver.findElement(By.name("Country")));
select.selectByIndex(2);


Method Name :- selectByValue

Syntax:- select.selectByValue("Value");
Purpose:- It is again the same what we have discussed earlier, the only difference in this is that it ask for the value of the option rather the option text or index. It takes a parameter of String which is on of the value of Select element and it returns nothing.



Select select = new Select(driver.findElement(By.name("Country")));
select.selectByValue("CAD");


Method Name :- deselectByVisibleText

Syntax:- select.deselectByVisibleText("Text");
Purpose: To Deselect all options that display text matching the given argument.
Select select = new Select(driver.findElement(By.name("Country")));
select.deselectByVisibleText("Canada");

Method Name :- deselectByIndex

Syntax:- select.deselectByIndex(index);
Purpose:- To Deselect the option at the given index. The user has to provide the value of index.


Select select = new Select(driver.findElement(By.name("Country")));
select.deselectByValue(2);


Method Name :- deselectByValue

Syntax:- select.deselectByValue("Value");
Purpose:- To Deselect all options that have a value matching the given argument.

Select select = new Select(driver.findElement(By.name("Country")));
select.deselectByValue("CAD");

Method Name :- deselectAll

Syntax:- select.deselectAll();
Purpose:- To Clear all selected entries. This works only when the SELECT supports multiple selections. It throws NotImplemented Error if the "SELECT" does not support multiple selections. In select it mandatory to have an attribute multiple="multiple" Please check for the below example.


Select select = new Select(driver.findElement(By.name("Country")));
select.deselectAll();



Method Name :- isMultiple

Syntax:- select.isMultiple();
Returns:- Boolean
Purpose:- This tells whether the SELECT element support multiple selecting options at the same time or not. This accepts nothing but returns boolean value(true/false).



Select select = new Select(driver.findElement(By.name("Country")));
Boolean flag = select.isMultiple();
System.out.println(flag);



Method Name :- getOptions

Syntax:- select.isMultiple();
Returns:- List
Purpose:- Returns all the option elements displayed in this select tag (dropdown list)




Select select = new Select(driver.findElement(By.name("Country")));
List<WebElement> allOptions = select.getOptions();
for(WebElement webElement: allOptions)
{
System.out.println(webElement.getText());
}


Method Name :- getAllSelectedOptions

Syntax:- select.getAllSelectedOptions();
Returns:- List
Purpose:- It will return all the option elements that are selected in the select tag.





Select select = new Select(driver.findElement(By.name("Country")));
List<WebElement> allSelectedOptions = select.getAllSelectedOptions();
for(WebElement webElement: allSelectedOptions)
{
System.out.println(webElement.getText());
}


Method Name :- getFirstSelectedOption

Syntax:- select.getFirstSelectedOption();
Returns:- WebElement
Purpose:- It will return the first selected option in this select tag (or the currently selected option in a normal select)


Select select = new Select(driver.findElement(By.name("Country")));
WebElement firstSelectedOption = select.getFirstSelectedOption();
System.out.println(firstSelectedOption.getText());







Read More

Popular Posts