Saturday, 4 January 2020

SE - 45 - Execution from batch file

We can do batch execution in selenium by giving the execution path of testng.xml in batch file. The execution can be triggered through command too (CMD)

At the first step we need to include the classes in the testng.xml file which we want to execute via batch :

<suite name="Main Test Suite" verbose="2">
    <test name="TestNG Test Group">
        <classes>
        <class name="com.test.Test1"/>
        <class name="com.test.Test2"/>
        </classes>
    </test>
</suite>

After that we need to call the testng.xml file by giving the whole location and creating a .bat file . After giving the following commands in a text file , save it as .bat and execute it :
cd %pathofProject%
set classpath=%projectLocation%\bin;%projectLocation%\lib\*
java org.testng.TestNG %pathofProject%\testng.xml
pause


Friday, 3 January 2020

SE - 44 - How to open a new tab in a pre opened browser from selenium

There are 2 ways to open a new tab in browser which is previously opened.

1) Using Sendkeys method

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

2) Using Robot class to send keyboard keys : Ctrl + T

//Launch the first URL
driver.get("http://www.google.com");
 
//Use robot class to press Ctrl+t keys     
Robot robot = new Robot();                          
robot.keyPress(KeyEvent.VK_CONTROL); 
robot.keyPress(KeyEvent.VK_T); 
robot.keyRelease(KeyEvent.VK_CONTROL); 
robot.keyRelease(KeyEvent.VK_T);
 
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
 
//Launch URL in the new tab
driver.get("http://google.com");


3) Using JavaScrptExecutor

String link = "window.open('https://www.google.com','_blank');";
((JavascriptExecutor)driver).executeScript(link);
OR

((JavascriptExecutor) driver).executeScript("window.open();");

SE - 43 - Methods of Object class


Object class is the parent of all the classes in Java. This class is at the top of all the classes in JAVA. This class is inherited by many classes. Some of the important methods of Object class are :

public final Class getClass() -> returns the Class class object of this object. The Class class can further be used to get the metadata of this class.

public int hashCode() -> returns the hashcode number for this object.

public boolean equals(Object obj) -> compares the given object to this object.

protected Object clone() throws CloneNotSupportedException -> creates and returns the exact copy (clone) of this object.

public String toString() -> returns the string representation of this object.

public final void notify() -> wakes up single thread, waiting on this object's monitor.

public final void notifyAll() -> wakes up all the threads, waiting on this object's monitor.

public final void wait()throws InterruptedException -> causes the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method).

protected void finalize()throws Throwable -> is invoked by the garbage collector before object is being garbage collected.