Tuesday, 7 August 2018

SE - 33 - Can we use multiple conditions when we apply Explicit wait

We can apply multiple conditions while implementing explicit wait in webdriver. We can give multiple conditions by using OR\AND with ExpectedConditions class. Example below :


public class MultipleWaits {

 public void mulwwait() {
  WebDriver driver = new ChromeDriver();
  
  WebDriverWait Wait = new WebDriverWait(driver , 10);
  Wait.until(ExpectedConditions.and(          //here AND\OR conditions can be applied here
    ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Path1")),
    ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Path1")),
    ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("Path1"))
    )
    );
 }
}
In the above example I have used AND conditions for giving multiple conditions to webdriver explicit wait. We can also use OR in place of it.

SE - 32 - Can we have Constructors inside constructor(or constructor chaining)

Yes we can have multiple constructors a class with different signatures. Also we can call the constructor from the constructor using this keyword :

See the example below :

public class ConstInConst {
// constructor 1
 ConstInConst(){
  this(1,2); //calling constructor 2
 }
//constructor 2
 ConstInConst(int i, int j) {
 this(4,5,6); // calling constructor 3
 System.out.println(i +" and "+ j);
 }
//constructor 3
 ConstInConst(int i, int j, int k) {
 System.out.println(i +" and "+ j + " and " + k);
 }

 public static void main(String[] args) {
  new ConstInConst();
 }
}
Output : 
4 and 5 and 6
1 and 2


In the above example we have created 3 constructors and called the second constructor in first and third in second. This term is also called constructor chaining. We can't call 2 constructors in a single constructor(only one is allowed at a time) . It gives error because the first line should call the constructor. 



Sunday, 5 August 2018

SE - 31 - Difference between .equals() and "==" operator


=> .equals
i) Its a method
ii) It can only be used for object comparison
iii) It does not looks for memory reference rather it looks for the objects(contents)

=> "=="
i) Its an operator (equality operator)
ii) It can be used for primitive as well as object comparison
iii) "==" compare two objects based on their memory reference(or address). It will return true only if two object has same reference or the reference it is comparing represent exactly same object . In the situation where the object reference are different then it will return false.

Example :

public class equalDotEquals {

 @Test
 public void equals() {
  
  Integer I1 = new Integer(10);
  Integer I2 = new Integer(10);
  System.out.println((I1==I2));
  System.out.println((I1.equals(I2)));
  //Now we are making the same reference
  I1=I2;
  System.out.println("-----------------------");
  System.out.println((I1==I2));
  System.out.println((I1.equals(I2)));
 }
}
Output of above program :

false
true
-----------------------
true
true


Here we can see that we created two objects I1 and I2  of Integer. We applied "==" and ".equals()" operator on it. The output of "== " is false but output of ".equals()" is true . The reason is that "==" is using reference for comparison but reference of both the objects are different so its showing false. But equal() method compares the content(object) so its showing true.
Now we made the references of both the variable same by using I1=I2. now both the operators are giving same output.

Wednesday, 1 August 2018

SE - 30 - Setting timer for a page to load completely by JavaScriptExecutor


Apart from waits there is one more synchronisation point through which we can check that the page is loaded or not or we can set a timeout for it. This can be achieved through java script executor.

Below code demonstrates that how we can use Javascriptexecutor to set a timeout and check that the page is loaded :
public void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    return ((JavascriptExecutor)driver)
                      .executeScript("return document.readyState").equals("complete");
                }
            };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);

So here we can see that we are checking the document.readystate should be equal to true. We have kept this inside the expected condition and we are using this with webdriver explicitwait.