User:शिव साहिल

From Simple English Wikipedia, the free encyclopedia

The following are the Expected Conditions that can be used in Explicit Wait

alertIsPresent() elementSelectionStateToBe() elementToBeClickable() elementToBeSelected() frameToBeAvaliableAndSwitchToIt() invisibilityOfTheElementLocated() invisibilityOfElementWithText() presenceOfAllElementsLocatedBy() presenceOfElementLocated() textToBePresentInElement() textToBePresentInElementLocated() textToBePresentInElementValue() titleIs() titleContains() visibilityOf() visibilityOfAllElements() visibilityOfAllElementsLocatedBy() visibilityOfElementLocated()




Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(5, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);

public WebElement apply(WebDriver driver) {

       return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));

1 executor.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element2);


1.1 WebElement element1 = driver.findElement(By.xpath("//eds-button[@icon='apps']"));

JavascriptExecutor executor = (JavascriptExecutor)driver;

executor.executeScript("arguments[0].click();", element1);

-->{{element not no interactable}}



2 Stale element

3 driver.get("chrome://settings/clearBrowserData");


3 Actions action = new Actions(driver);

4 action.moveToElement(element2).click(element2).build().perform();

framework


[[elementfrompoint is not a funciton]] -->focus
// Base class

package Academy;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class base {
/*we are going to write a re-usable method to initialize the Driver
for invoking drive we are going to create a single method, and all the testcases are mapping to this single...
method for invoking a browser(moreover changing browser become easy) and we can give  timeout
*/
	
public static WebDriver driver; //defining driver as global, cause we want to have scope of driver in all this class and in the class which will inherit or use it using object of this class,	
// we are giving it as static because in the end we are again initializing the driver as null for saving memory. Static says we are using same driver over and over again. We can't create one more driver until it is nullified.

public Properties prop; // we are defining it outside cause we need to use prop.getProperty("URL") in test classes (i.e. assertions,homePag class) . if we keep on giving 'prop' file within the method then it's scope will not come out of that method

public WebDriver initilaizedriver() throws IOException //here return type is WebDriver
{
	
	// first thing to do is to get browser preference using .properties file and invoke that broswer
	// create .properties file in any of the package in the src/main/java folder and give browser=chrome with name universaldata.properties
	
	prop= new Properties();
	FileInputStream fis=new FileInputStream("C:\\Users\\Sahil\\Project124\\src\\main\\java\\Academy\\universaldata.properties"); 
	prop.load(fis);      
	String browser_name=prop.getProperty("browser");
	if (browser_name.equals("chrome"))
	// earlier I was writing as browser_name=="chrome". We can't use == operator when using properties file. 
	// == is used when both variable in same memory and 'equals' used when both things in different memory	
	{
		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
		driver=new ChromeDriver(); // we have not written WebDriver driver, we have define it as global earlier
	}
	
	
	else if (browser_name.equals("firefox"))
	{
		
	}
	
	
	else if (browser_name.equals("IE"))
	{
		
	}
		
	driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //this will be applicable for whole framework
	/* if some error is coming from timeout line, telling Null pointer exception then it means that none  
	of the if-else condition are getting hit.*/
	return driver;
	
	
}

public void screenshot(String result) throws IOException // it is being called by listener, it is expecting "result". Result contain name of the method which has failed & we are going to add it to the screenshot name	
{
	String timeStamp;
	File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
	timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());  
	FileUtils.copyFile(scrFile, new File("C:\\Users\\Sahil\\Desktop\\"+result+"_"+timeStamp+".png"));
}



}
----------------------------------------------------
// properties file

browser=chrome
url1=https://sso.teachable.com/secure/9521/users/sign_in?clean_login=true&reset_purchase_session=1
url2=https://www.google.com/


--------------------------------------------------
// POP lnding_page

package POP;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class landing_page {

	public WebDriver driver; /*here we have created driver again but it doesn't have capabilities to invoke chrome etc
	 if we run like this then we will get Null pointer error, we need to add constructor to get desired capabilities
	 constructor will take driver from homepage where driver have chrome capabilities. It will transfer the capabilities by using this.driver=driver*/
	
	//you might have noticed that here we are creating driver but we haven't done so in homePage or googleassert cause there was no need in the classes which has inherited base class. Base class has driver
	
	By name=By.xpath("//*[@id=\'user_email\']");
	By pwd=By.xpath("//*[@id=\'user_password\']");

	
	public landing_page(WebDriver driver) {  // when we are creating the object of this clas then we are also invoking this Parameterized constructor 
		this.driver=driver; // driver is driver from homepage while this.driver is driver available here.
	}


	public WebElement name()   //see the return type is a WebElement not even Webdriver
	{
		return driver.findElement(name);  // here name=By.xpath("//*[@id=\'email\']").
		// it will look like driver.findElement(By.xpath("//*[@id=\'email\']"))
	}
	
	public WebElement pwd()
	{
		return driver.findElement(pwd);
	}
	
}
--------------------------------------------------------
// POP googeassert

package POP;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class googleassert {
	public WebDriver driver;
	
	By name=By.xpath("//*[@id=\'gbw\']/div/div/div[1]/div[1]/a");
	By search=By.xpath("//*[@id=\'tsf\']/div[2]/div/div[1]/div/div[1]/input");

	
	public googleassert(WebDriver driver) {
		this.driver=driver; // driver is driver from homepage while this.driver is driver available here.
	}


	public WebElement name() //see the return type is a WebElement not even Webdriver
	{
		return driver.findElement(name);  // here name=By.xpath("//*[@id=\'email\']").
		// it will look like driver.findElement(By.xpath("//*[@id=\'email\']"))
	}
	
	public WebElement Search()
	{
		return driver.findElement(search); 
	}
}
--------------------------------------------------------------------------
// POP login second


package POP;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class login_second {
	
	public WebDriver driver;
	By image=By.xpath("//*[@id=\'navbar\']/div/div/a/img");
	public login_second(WebDriver driver) {
		this.driver=driver;
	}

	public WebElement image()
	{
	}
	

}
----------------------------------------------------------------
//homePage test class

package Academy;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import POP.landing_page;
import POP.login_second;

public class homePage extends base{ //here we are inheriting base class for driver and prop

public static int a=0;  
	
@BeforeMethod
public void first() throws IOException
{
	driver=initilaizedriver(); //initilaizedriver give us driver & we are receiving it. 
	// If we were creating a object ofbase then we would havt to write obj.initilaizedriver() to access it, But in same class and child class such arrangement can be made
	//driver.get("https://sso.teachable.com/secure/9521/users/sign_in?clean_login=true&reset_purchase_session=1");  ---> we are employing the driver we get from initializedriver
	driver.get(prop.getProperty("url1"));  // we are able to use prop here cause we have given prop as public in base class
	
	
	
}
	

@Test(dataProvider="senddata")    // giving data provider here.
public void testcase1 (String user, String pwd) throws IOException  //user and pwd is way to catching data from data provide
{
	
	
	landing_page l=new landing_page(driver);  // here we are NOT inheriting landing_page class like in case of base class for getting driver, instead we are using it by creating a object of it..
	/* we are adding driver above to get it to the constructor of landing_page so that we can pass driver which is available here (it has chrome capabilites) to next page
	 after you add driver you will get an error, hover and select create constructor.*/
	
	l.name().sendKeys(user);
	l.pwd().sendKeys(pwd);
	l.pwd().sendKeys(Keys.ENTER);
	login_second ls=new login_second(driver);
	ls.image().click();
	
}


@AfterMethod
public void closethebrowser()
{
	driver.quit();  // i am not using AfterTest or AfterClass cause all the browserS will close after all the test folder or whole whole class is executed. In this way browsers will be executed after each method
	driver = null; // we are initalizing driver for each and every method. So in order to save the memory by bringing the driver back to null;
	// if we are giving it as null then during the initialization make it "Static"
}


@DataProvider 					    
public Object[][] senddata()		
{
	Object[][] data1=new Object[3][2]; // Important:- instead of writing Object[2][2], for giving two data we are giving [3][2]
	data1[0][0]="abc@gmai.com";   
	data1[0][1]="123";
	data1[1][0]="xyz@gmai.com";
	data1[1][1]="52";
	data1[2][0]="lmn@gmai.com";
	data1[2][1]="11";
	return data1;	
	
	// testcase1 method will run as many time as we send the data i.e. rows!!!
	// After testcase1 class is run with first row's data then testcase1 will run again with second row's data ****
	// different chrome windows will open for each row
}
}
------------------------------------------------------------------
// assert test class

package Academy;

import static org.testng.Assert.assertEquals;

import java.io.IOException;

import org.openqa.selenium.Keys;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import POP.googleassert;
import POP.landing_page;
import POP.login_second;



public class assertions extends base{
	
	
	@BeforeMethod
	public void first() throws IOException
	{
		driver=initilaizedriver();  // initilaizedriver() is giving us driver, moreover we don't need a object to use this class, How easy is this.
		// driver.get("https://www.google.com/"); ---> We need to use properties file to give url here. We have properties file in the base.
		driver.get(prop.getProperty("url2"));  // we are able to use prop here cause we have given prop as public in base class
		
	}
	
	
	@Test   
	public void testcase2 () throws IOException  
	{
		googleassert ga=new googleassert(driver); 
		System.out.println(ga.name().getText());
		
		Assert.assertTrue(ga.name().isDisplayed()); // there is also Assert.assertFalse(condition) which is expecting the condition to be fail
		// if condition is opposite then whole script fails
		Assert.assertEquals(ga.name().getText(), "gmail");  // this is Assert not assert. Assert is a feature of TestNG
		// both expected and original doesn't match then script fails!!! Yes literally script fails
		
		ga.Search().sendKeys("abc");


	}
	
	@AfterMethod
	public void closethebrowser()
	{
		driver.quit();  // i am not using AfterTest or AfterClass cause all the browser will close after all the test folder or whole whole class is executed. In this way browsers will be executed after each method
		driver = null; // we are initlaizing driver for each and every method. So inorder to save the memory we are bringing the driver back to null;
	}
	
	
}
------------------------------------------------------------------
// listener

package Academy;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class Help_me_listen implements ITestListener {
	
	base b=new base();   // we need to access the base class methods here. Method we need to access is of screenshot

	@Override
	public void onTestStart(ITestResult result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTestSuccess(ITestResult result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTestFailure(ITestResult result) {
		
		try {
			b.screenshot(result.getName());	//result.getName() will give name of the method which has failed and we are passing it to the screenshot method. it will go to base class
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		// as I wrote b.screenshot() eclipse told me to write it in try catch block, above thing came from eclipse itself.
	}

	@Override
	public void onTestSkipped(ITestResult result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStart(ITestContext context) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFinish(ITestContext context) {
		// TODO Auto-generated method stub
		
	}



}
-----------------------------------------------------------------
<!-- pom xml -->

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Academy</groupId>
  <artifactId>Project124</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Project124</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

    <build>                                                    
    <pluginManagement>
      <plugins>
        <plugin>                                                                      
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>

<configuration>
  <suiteXmlFiles>
    <suiteXmlFile>testng.xml</suiteXmlFile> 
  </suiteXmlFiles>
</configuration>

      </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencies>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.1.1</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<!-- pom.xml -->
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.1.5</version>
</dependency>
<dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.41.2</version>
</dependency>
  </dependencies>
</project>
---------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
	<listener class-name="Academy.Help_me_listen"/>  <!-- we need to give exact location of listner-->
</listeners>
  <test name="Test">
    <classes>
      <class name="Academy.assertions"/>
     <class name="Academy.homePage"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
------------------------------------------------------<syntaxhighlight lang="text">

44. Full Framework

  1. Step 1:- Maven

Open CMD at location where you want to create the project paste it in the command prompt.

mvn archetype:generate -DgroupId=Academy -DartifactId=Project124 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

D represent that we are going to add a parameter. groupId is a parameter for this command. It work or any JAVA related project. For developer and mobile projects DinteractiveMode is 'true'. Academy (groupId) is under 'main' folder as well as in 'test'.

  1. Step2:- Check whether the Project is created on the desired location.
  2. Step3:- Connect with Eclipse.

--open command prompt in folder (just created) in which our pom.xml is lying. -- give command mvn eclipse:eclipse. By using this the file will be automatically generated --IMPORT: File>>Import>>Maven>>Existing Maven Project>>Select the folder where the project has been created after installation>>Finish

  1. Step4:- get dependencies and paste in the maven like selenium, chrome driver and testNG
  2. Step5:- We have two folder src/test/java and src/main/java/ . In "test" folder we are going to develop testcases and in "main" folder we are giving POP objects, Base class** etc.

We are going to write base class in "main" folder. Check the code for the commentary -->Use of a .properties file for getting browser preference and invoke that browser in base class. --> we are then going to give implicit timeout too in base class.

  1. Step6:- create a new class (homePage) in "test" folder. Inherit Base class in that. Driver will also get inherited. just open the website. Here we are just checking that whatever have been written is working fine or not.
  2. Step7:- Create Page Objects classes in "main". create those classes under new package. And add them to test class(homePage). We are inheriting the "base" class using the Homepage class not directly from base class.

we are creating object in POP class and we are accessing POP's methods in homepage class.

  1. Step8:- We have given user_name and pwd in sendkeys, but now we will be giving them in data provider annotation in homepage class itself.
  2. Step9:- Do assertions in NEW test class called assertions.
  3. Step10:- we have given URL in the testcase we need to give them from the properties file.
  4. Step11:- create TestNg file, by right clicking on project>>TestNG>>convert to testNG
  5. Step12:- Close all the browsers by giving @AfterMethod annotations. Also add @BeforeMethod for URL
  6. Step13:- integrate testNG with maven.
  7. Step14:- Open cmd on pom.xml position, "maven test"
  8. Step15:- Create testNG listener. add testng Listeners into testNG
  9. Step16:- add screenshot jar in maven repository and write the code for it in base class and call it in the listener where it can pass which method has failed.
  10. Step17:- extends report

-- go to http://extentreports.com/>>Docs>>Version 3 as well as 2>>Java>>Copy the dependency and paste in maven --go to http://extentreports.com/docs/versions/3/java/ >>example>>TestNg Example. read some of it. From this a file has been created, and paste it in the same package as 'base' class

???? ????