I have planned to customize the testNG reports. So I have used ExtentReports with the below codes.
Selenium runs properly without any issues but the report is not generated in the specified folder location.
I have added ExtentReport 2.41.2 maven dependency in my pom.xml file.
Sample Code:
public class ExtentA
{
public static ExtentReports extent;
public static ExtentTest logger;
public static WebDriver driver;
#BeforeSuite
public void config()
{
extent = new ExtentReports("E:/Automation/MyReport.html", true);
extent.loadConfig(new File("E:/Automation/extentreports-java-2.41.2/extentreports-java-2.41.2/extent-config.xml"));
}
#BeforeMethod
public void beforeMtd(Method method)
{
logger = extent.startTest("sample", "test case desc");
logger.assignAuthor("Mohan");
}
#Test
public void sample()
{
System.setProperty("webdriver.chrome.driver", "C:/selenium/drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.co.in");
logger.log(LogStatus.PASS, "Browser Launched successfully");
System.out.println("Browser launched");
driver.manage().window().maximize();
}
#AfterMethod
public void close()
{
driver.close();
driver.quit();
extent.endTest(logger);
}
}
Before running the test case, I have created the html file in loca, but the report is not generated.
Add extent.flush(); to the end of your close method.
Related
I'm running my selenium code for Page object model using testNG, #BeforeTest will run only once for every test and #BeforeMethod will run everytime for every test. So which annotation would be preferable to use for page object model ?
#BeforeMethod
public void setUp() throws IOException
{
initialisation();
loginPage = new Login();
loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
ot = new OpenTasks();
active = new ActiveProjects();
}
#Test(priority=1)
public void validateTitle() throws IOException, InterruptedException
{
String custTitle = ot.verifyTitle();
Assert.assertEquals(custTitle, "actiTIME - Open Tasks", "Title not matched");
}
#Test(priority=2)
public void verifyProjectLink() throws IOException, InterruptedException
{
active = ot.clickOnProjectLink();
}
#AfterMethod
public void tearDown()
{
driver.quit();
}
(or)
public static WebDriver driver;
#BeforeTest
public void preConfig()
{
if(browser.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver","C:\\Program Files\\FireFox\\geckodriver-v0.24.0-win64\\geckodriver.exe");
driver=new FirefoxDriver();
}
if(browser.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Chrome\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
}
driver.get("http://desktop-g53h9ip:81/login.do");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#AfterTest
public void postConfig()
{
driver.close();
}
It is better to instantiate browser once for a single test and 2nd approach is better approach for any kind of framework.
Following structure for TestNG annotations
#BeforeSuite
#BeforeTest
#BeforeClass
#BeforeMethod
#Test
#AfterMethod
#AfterClass
#AfterTest
#AfterSuite
Below is the code am trying to execute
Not sure why am getting [TestNG] No tests found. Nothing was run
if I remove the before class annotation method, it executes but fails due the dependency
public class TestNG_Practice3 {
static WebDriver driver ;
String url = "https://in.linkedin.com/";
#BeforeClass(description = "To open the browser")
public void openBrowser()
{ driver = new FirefoxDriver();
driver.get(url);
System.out.println("Browser got open");
}
#Test (dependsOnMethods ="openBrowser",description = "To signin")
public void login()
{
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
WebElement signin = driver.findElement(By.id("login-email"));
Assert.assertTrue(signin.isDisplayed());
WebElement password = driver.findElement(By.id("login-password"));
WebElement signinbutton = driver.findElement(By.id("login-submit"));
signin.sendKeys("xyz");
password.sendKeys("abc");
signinbutton.click();
Assert.assertTrue(driver.getCurrentUrl().contains("feed/"));
}
#Test(dependsOnMethods = "login")
public void logout()
{
WebElement meDropdown = driver.findElement(By.xpath("//*[#id=\"nav-settings__dropdown-trigger\"]/div/span[2]/li-icon/svg"));
meDropdown.click();
WebElement logout = driver.findElement(By.id("ember545"));
logout.click();
}
#AfterClass
public void closebrowser()
{
driver.quit();
}
}
Step-1 : Basic trial with Project Build,
public class TestNG_Demo {
#BeforeClass
public void openbrowser()
{
System.out.println("Browser got open");
}
#Test
public void testbrowser()
{
System.out.println("Test execution");
}
#AfterClass
public void closebrowser()
{
System.out.println("Browser got close");
}
}
So you will be have idea, Your project build get successful execution.
If you have maven project and Build did not get pass, you will be have trigger what is causing from maven build dependency.
Update
Step-2 : After tracing first trial
public class TestNG_Demo {
#Test
public void testbrowser()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
}
}
Remove dependsOnMethods ="openBrowser" because it's not a test method and will be execute before test without it
I wrote a cucumber framework that has two feature files and two stepdefinitions which are glued to the feature files. When i run the test together, it ran for the first stepDefinition and fail to enter the second stepDefinition. I have initialize my pages and yet couldn't get it to work.
Error and Codes below
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy19.click(Unknown Source)
at Pages.HomePage.CreateANewOrderPage.createOrderLink(CreateANewOrderPage.java:35)
at StepDefinitions.CreateOrderStep.user_click_on_create_a_new_order(CreateOrderStep.java:24)
at ✽.When user click on create a new order(CreateOrder.feature:5)
public class CreateANewOrderPage {
WebDriver driver;
public CreateANewOrdePage(WebDriver driver){
this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy (linkText= "Create a new order")
public WebElement createOrderLink;
public void createOrderLink(){
createOrderLink.click();
}
public class SigninPage {
WebDriver driver;
public SigninPage(WebDriver driver) {
this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(xpath="//*[#id=\"userName\"]")
public WebElement usernameField;
#FindBy(name="password")
public WebElement passwordField;
#FindBy(id="buttonSubmitLogin")
public WebElement submitBtn;
public void loginDetails(String uname, String psw) {
usernameField.sendKeys(uname);
passwordField.sendKeys(psw);}
public void clickLogin(){
submitBtn.click();
}
}
public class SigninStep {
WebDriver driver;
SigninPage logIn = new SigninPage(driver);
#Given("^user navigates to mySite$")
public void userNavigatesToMysite() throws Throwable {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\mypc\\Documents\\Automation\\drivers\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("www.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#And("^user enter \"([^\"]*)\" and \"([^\"]*)\"$")
public void userEnterValidCredentials(String validuname, String validpsw) throws Throwable {
SigninPage logIn = new SigninPage(driver);
logIn.loginDetails("jdjdjdj","jjdjdj");
}
#When("^user click on Sign in$")
public void userClickSignIn() throws Throwable {
SigninPage logIn = new SigninPage(driver);
logIn.clickLogin();
}
}
public class CreateOrderStep {
WebDriver driver;
CreateANewOrderPage ordercreate;
#When("^user click on create a new order$")
public void user_click_on_create_a_new_order() throws Throwable {
ordercreate= PageFactory.initElements(driver,CreateANewOrderPage.class);
ordercreate.createOrderLink();
}
#RunWith(Cucumber.class)
#CucumberOptions (features = "src\\test\\java\\Features\\",
glue ={"StepDefinitions"},
tags={"#Signin, #CreateOrder"}
//format = {"pretty", "html:target/Destination.."}
// format={"json:target/Destination/cucumber.json"
)
public class SigninRunner {
}
I have downloaded all the JUnit jar files and installed them.When I run my package as JUnit,it says no tests found with TestRunner 'JUnit 4'.What am I missing
*public class AmazonTestFactory{
static WebDriver driver;
#Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
driver.get("http://www.amazon.co.uk");
Thread.sleep(5000);
driver.manage().window().maximize();
}
#Test
public void testHomePage(){
AmazonHomePageFactory Page = PageFactory.initElements(driver, PageFactoryModelExercise.AmazonHomePageFactory.class);
Page.enterProductInSearchBox();
Page.clickSearchButton();
}
#After
public void closeBrowser(){
driver.close();
}*
I had tried to run below JUnit (Selenium WebDriver) test case to open Google in Chrome browser, but it is failing with error message as
"The path to the ChromeDriver executable must be set by the
webdriver.chrome.driver system property; for more information, see
http://code.google.com/p/selenium/wiki/ChromeDriver."
As specified in that website, I downloaded ChromeDriver.exe but don't know,
Which PATH should I place that? or
How to set ChromeDriver path in webdriver.chrome.driver?
Please Advise.
My JUnit test case (changed the Firefox Driver to Chrome Driver):
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Chrome_Open_Google {
private WebDriver driver;
private String baseUrl;
#Test
public void Test_Google_Chrome() throws Exception {
driver = new ChromeDriver();
baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
I believe you have several options:
Either specify the folder (in which your chromedriver binary is) in your PATH system variable - here's how
Or give you application webdriver.chrome.driver as a system property by calling it with -Dwebdriver.chrome.driver=the/path/to/it parameter.
Or the same programatically: System.setProperty("webdriver.chrome.driver", "your/path/to/it");
Or this:
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
.usingAnyFreePort()
.build();
service.start();
}
#Before
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#AfterClass
public static void createAndStopService() {
service.stop();
}
System.setProperty("webdriver.chrome.driver", "your\path\to\it");
For Eg :
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();