Wednesday, 12 February 2020

macOS - selenium with chrome browser




download chromedriver.exe and run below code - rest all same as windows


@org.testng.annotations.Test
public void m2() {
System.setProperty("webdriver.chrome.driver",
"/Users/kk/Documents/workspace/test/driver/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");

}

macOS - Safari browser : selenium setup

Install Java
Install Eclipse
Install TestNg
Create Maven project - add selenium-java & TestNG dependencies

1. Open Safari Browser and look out for Develop Menu
     if you do not found Develop menu , you had to enable it by following below step
     Safari->Preferences -> Advanced Tab -> Select 'Show Develop menu in menu bar'
2. Develop menu -> Allow Remote Automation

Thats all - Here is the Program for safari'

        @Test
public void m1(){
System.out.println("test");
WebDriver driver = new SafariDriver();
driver.get("http://www.facebook.com");

}

Run it

To summarise, we do not need any driver.exe file needs to be downloaded
and configured!

Thursday, 23 January 2020

core java interview questions

Can we Override static methods in java? - NO
If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class

Can we overload static methods? - Yes
We can have two ore more static methods with same name, but differences in input parameters.

what is factory design pattern?
Pattern provides one of the best ways to create an object - We create object without exposing the creation logic to the client and refer to newly created object using a common interface.

https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

how you initialize Page factory object
https://www.seleniumeasy.com/selenium-tutorials/page-factory-pattern-in-selenium-webdriver

what is POM design pattern?
https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html


is java pure object oriented?  - NO
- because it supports primitive data type [Non Object types] like byte,short,int,long,float,double,char,boolean,which are not objects.
- evrything in java is class, except primitive datatypes.still wrapper classess are there..for this reason we can say java is not fully object oriented language
-The main difference between primitive and reference type is that primitive type always has a value, it can never be null but reference type can be null, which denotes the absence of value
-the main difference between two types is that primitive types store actual values but reference type stores handle to object in the heap
-modification on one primitive variable doesn't affect the copy but it does in the case of the reference variable

int i = 20; int j = i; j++; // will not affect i, j will be 21 but i will still be 20
List<String> list = new ArrayList(2);
List<String> copy = list;
copy.add("EUR");
// adding a new element into list, it would be visible to both list and copy
System.out.printf("value of list and copy after modification list: %s, copy: %s %n", list, copy);




staic class :
Static classes are basically a way of grouping classes together in Java. Java doesn't allow you to create top-level static classes; only nested (inner) static classes.
 public class CarParts {
 
    public static class Wheel {
        public Wheel() {
            System.out.println("Wheel created!");
        }
    }

    public CarParts() {
        System.out.println("Car Parts object created!");
    }
}

wrapper class:
Each Java primitive has a corresponding wrapper:
boolean, byte, short, char, int, long, float, double
Boolean, Byte, Short, Character, Integer, Long, Float, Double
These are all defined in the java.lang package, hence we don't need to import them manually.

Basically, generic classes (Collection Framework)only work with objects and don't support primitives. As a result, if we want to work with them, we have to convert primitive values into wrapper objects.

heap memory vs stack memory:
stack memory is used to store local variables and function call
heap memory is used to store objects in Java
Each Thread in Java has their own stack which can be specified using
If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError:
If you are using Recursion, on which method calls itself, You can quickly fill up stack memory. Another difference between stack and heap is that size of stack memory is a lot lesser than the size of  heap memory in Java.
ariables stored in stacks are only visible to the owner Thread while objects created in the heap are visible to all thread. In other words, stack memory is kind of private memory of Java Threads while heap memory is shared among all threads.

== vs equals
We can use == operators for reference comparison (address comparison) and .equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.
String s1 = new String("HELLO");
String s2 = new String("HELLO");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //True

Friday, 2 August 2019

ElementNotInteractableException



ElementNotInteractableException::
is caused when an element is found, but you can not interact with it. For instance, you may not be able to click or send keys.

There could be several reasons for this:

1. Wait until an element is visible / clickable
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf(element));
wait.until(ExpectedConditions.elementToBeClickable(element));

2. Add Implicit wait

what is stale element exception - how to fix it


Stale = left over / old

if an element changes its position and then if you are trying to perform some event on top of it then we get stale element exception.

below are the fixes

wait.until(ExpectedConditions.visiblityofElementToBeClickable(By.id(“id”)))

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id")));

wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf("table")));

Interview qns on final keyword | java

1) Java final variable
If you make any variable as final, you cannot change the value of final variable(It will be constant).

2) Java final method
If you make any method as final, you cannot override it.

3)Java final class
If you make any class as final, you cannot extend it.

Q)Is final method inherited?
Ans) Yes, final method is inherited but you cannot override it. For Example:

Q)Can we initialize blank final variable?
Yes, but only in constructor. For example:

Q)static blank final variable
A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Q) What is final parameter?
If you declare any parameter as final, you cannot change the value of it.   int cube(final int n){ n = n+2; }

Q)Can we declare a constructor final?
No, because constructor is never inherited.

Thursday, 1 August 2019

Print your name 10 k time with out for loop

  1. String name = "kranthi";
  2. String str = "X";
  3. str = str.replaceAll("X", "XXXXXXXXXX");
  4. str = str.replaceAll("X", "XXXXXXXXXX");
  5. str = str.replaceAll("X", "XXXXXXXXXX");
  6. str = str.replaceAll("X", name + "\n");
  7. System.out.println(str);

or


@Test(invocationcount=10)
public void main(){
SYstem.out.println("hello")
}