One of the #Test code block doesn't execute - selenium-webdriver

The TocheckApproval() code block doesnot get executed
Can someone please suggest why this is happening and what steps do i need to take to execute both the #Test blocks

I'm not sure whether method order is guaranteed by TestNG framework.
If you want the method order like run the test only after some tests ran (and marked as PASS), you can use dependsOnMethods or dependsOnGroups
Eg:
import org.junit.FixMethodOrder;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
public class TestExample {
#BeforeTest
public void beforetest(){
System.out.println("before test ");
}
#Test
public void Tocheckapproval() {
System.out.println("in the method: Tocheckapproval");
}
#Test(dependsOnMethods = {"Tocheckapproval"})
public void TocheckRequestDecline() {
System.out.println("in the method: TocheckRequestDecline");
}
#AfterTest
public void aftertest() {
System.out.println("after test");
}
}
If you want to define the method order of your choice and the tests should not depend on other tests, then instead of dependecyOn* (methods, groups), you can use method Interceptors.
method Interceptors provides the list of test methods that are going to run as one of the argument, then you can re-arrange as per your needs.
References:
http://testng.org/doc/documentation-main.html#methodinterceptors
http://beust.com/weblog/2008/03/29/test-method-priorities-in-testng/

You haven't set any test priorities, that's why!
This is very easy to do, just stick the priority next to your #Test tag.
#Test (priority=1)
public void ToTestApproval(){
//do some stuff
}
#Test (priority=2)
public void ToCheckRequestDecline(){
//do some more stuff
}
As Naveen, mentioned above you can further refine the desired behaviour by using dependsOnMethods and dependsOnGroups.
PS. If this does not work, then it could be down to the #Test tag itself. Please double check that you are using TestNG annotation instead of Junit!
Best of luck!

Related

Test Case breaking when running the application as a TestNG test

I wrote a java application to use the selenium webdriver to automate few web application tasks. They all worked fine when run as a Java application.
To use the TestNG reporting feature, I ran the application as a TestNG test instead of JAVA application. The same test which was working as JAVA application is failing when I run as testNG.
However, I'm guessing I've setup the TestNG properly since the first testcase which is used to login to the webapp is passing (webLogin method in the code below)
I'm using chromeDriver. I tried removing the main method to run it as testNG application. But it did not work. I made sure that the element path locators I'm using are still valid while using testNG.
package myPackage;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import com.google.common.base.Function;
public class checkNavigation {
public static WebDriver driver;
public static WebDriverWait wait;
public static void main(String args[]) throws InterruptedException{
Configure();
wait = new WebDriverWait(driver, 30);
webLogin();
addClient();
addGoal();
addInsurance();
validateInputs();
endSession();
}
#BeforeTest
public static void Configure() {
System.setProperty("webdriver.chrome.driver", "/Users/divyakapa/Desktop/automation/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.get("https://example.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public static void webLogin() {
getElement(By.xpath("//*[#id=\"id\"]")).sendKeys("admin");
getElement(By.xpath("//*[#id=\"pw\"]")).sendKeys("password");
getElement(By.xpath("//*[#id=\"ember383\"]/div/div/form/button/span")).click();
}
#Test
public static void addClient() {
getElement(By.xpath("//*[#id=\"ember744\"]/button/div")).click();
getElement(By.xpath("//*[#id=\"ember744\"]/div/button[1]/div[2]/div")).click();
getElement(By.xpath("//*[#id=\"newInputFirst\"]")).sendKeys("firstName");
getElement(By.xpath("//*[#id=\"newInputLast\"]")).sendKeys("lastName");
getElement(By.xpath("//*[#id=\"newPersonInputBirthday\"]")).sendKeys("1991");
Select location = new Select(driver.findElement(By.xpath("//*[#id=\"newUserInputProvince\"]")));
location.selectByVisibleText("Place1");
Select isRetired = new Select(driver.findElement(By.xpath("//*[#id=\"alreadyRetiredDropdown\"]")));
isRetired.selectByVisibleText("No");
Select age = new Select(driver.findElement(By.xpath("//*[#id=\"newRetirementAge\"]")));
age.selectByVisibleText("60");
getElement(By.xpath("//*[#id=\"data-entry-modal\"]/div[2]/div/div[1]/div[2]/button[2]")).click();
}
#Test
public static void addGoal() {
getElement(By.xpath("//*[#id=\"ember2328\"]/button/div")).click();
getElement(By.xpath("//*[#id=\"ember2328\"]/div/div[1]/div[2]/button[3]")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"ember2464\"]/ul/li[1]/div/div/div[2]/div/span"))).click();
getElement(By.xpath("//*[#id=\"basicExpenseInputAmount\"]")).clear();
getElement(By.xpath("//*[#id=\"basicExpenseInputAmount\"]")).sendKeys("90000");
getElement(By.xpath("//*[#id=\"ember2563\"]/div/div[2]/div[2]/button[2]")).click();
// Add income
getElement(By.xpath("//*[#class=\"add-button \"]")).click();
getElement(By.xpath("//*[#data-test-model-type=\"income\"]")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class=\"list-group\"]/li[1]"))).click();
getElement(By.xpath("//*[#id=\"employmentInputName\"]")).clear();
getElement(By.xpath("//*[#id=\"employmentInputName\"]")).sendKeys("Company");
getElement(By.xpath("//*[#id=\"employmentInputSalary\"]")).sendKeys("95000");
getElement(By.xpath("//*[#id=\"employmentInputBonus\"]")).sendKeys("5000");
getElement(By.xpath("//*[#id=\"employmentInputBenefitsInKind\"]")).sendKeys("1000");
getElement(By.xpath("//*[#aria-label=\"Save\"]")).click();
}
#Test
public static void addInsurance() {
getElement(By.xpath("//*[#class=\"add-button \"]")).click();
getElement(By.xpath("//*[#data-test-model-type=\"protection\"]")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class=\"list-group\"]/li[1]"))).click();
getElement(By.xpath("//*[#id=\"termLifeName\"]")).clear();
getElement(By.xpath("//*[#id=\"termLifeName\"]")).sendKeys("BlueCrossBlueShield");
getElement(By.xpath("//*[#id=\"ukTermProtectionSalaryMultiplier\"]")).clear();
getElement(By.xpath("//*[#id=\"ukTermProtectionSalaryMultiplier\"]")).sendKeys("5");
Select empId = new Select(driver.findElement(By.xpath("//*[#id=\"termLifeInsuranceEmploymentId\"]")));
empId.selectByVisibleText("Company");
getElement(By.xpath("//*[#aria-label=\"Save\"]")).click();
}
#Test
public static void validateInputs() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if(!(driver.findElements(By.xpath("//*[#data-test-accordion-header=\"goals\"]")).size() > 0)) throw new NullPointerException("Income Failed to ADD");
if(!(driver.findElements(By.xpath("//*[#data-test-accordion-header=\"income\"]")).size() > 0)) throw new NullPointerException("Income Failed to ADD");
if(!(driver.findElements(By.xpath("//*[#data-test-accordion-header=\"protection\"]")).size() > 0)) throw new NullPointerException("Income Failed to ADD");
}
public static WebElement getElement(final By locator) {
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement>() {
#Override
public WebElement apply(WebDriver arg0) {
return arg0.findElement(locator);
}
});
return element;
}
#AfterTest
public static void endSession() {
driver.close();
driver.quit();
}
}
Running the above code, the get the following error :
Default suite
Total tests run: 5, Failures: 4, Skips: 0
I also see that it takes a lot of time (about 10 seconds) before the page is logged in even though that test passes. This does not happen when I run the code as a Java application
Can you show which tests are actually failing? if you are looking for order in testng test execution, it doesn't come by default , so if you have to run test2 after test1 and test3 after test2 etc then you have to use priority(lower the number higher the priority) for ex,
#Test(priority=1)
public void Test1() {
}
#Test(priority=2)
public void Test2() {
}
#Test(priority=3)
public void Test3() {
}
hope this helps
No, testng never guarantees ordering by default
TestNG relies on reflection. The Java Reflection APIs does not guarantee the method order when we use it to introspect a class to find out what are the test methods that are available in it. So the order of independent methods (Methods that dont have either soft or hard dependency) execution is never guaranteed.
Soft dependency - This is usually achieved in TestNG by using the priority attribute for the #Test annotation. Its called a soft dependency because TestNG will continue to execute all the methods even though a previous method with a higher priority failed.
Hard dependency - This is usually achieved in TestNG by using either dependsOnMethods (or) dependsOnGroups attribute for the #Test annotation. It's called a hard dependency because TestNG will continue to execute a downstream method if and only if an upstream method ran successfully.
By default testng executes methods in alphabetical order of the method names. Typically you would not be using main method for testng. Annotations along with priority are used to set the sequence of executions
Testng framework will run the test methods in alphabetical order. I could see your test methods are dependent and it should in the order. You can set the priorities for your test methods the way you want it to run.
You can refer the below link to set the priority.
TestNG priority set

How to inject a Drone instance without managing its lifecycle?

I have a Graphene Page Object.
#Location("/page")
public class MyPage {
#Drone
private WebDriver driver;
// page methods using the driver
}
And a Test Class that uses the page object.
#RunWith(Arquillian.class)
public class MyTest {
#Test
public void test(#InitialPage MyPage page) {
// use page & assert stuff
}
#Test
public void anotherTest(#InitialPage MyPage page) {
// use page & assert stuff even harder
}
}
Now, I've decided that MyTest should use method scoped Drone instances. So I add...
public class MyTest {
#Drone
#MethodLifecycle
private WebDriver driver;
Now when I run the test I get two browsers and all tests end with errors. Apparently this lifecycle management is treated as a qualifier too.
Yes, adding #MethodLifecycle in MyPage too helps. But this is not a solution - a page shouldn't care about this and should work in any WebDriver regardless of its scope. Only tests have the knowledge to manage the drone lifecycles. A page should just use whatever context it was invoked in. How can I achieve that?
This may be the answer:
public class MyPage {
#ArquillianResource
private WebDriver driver;
But I'm afraid that this skips some Drone-specific enriching. Also not sure if it will correctly resolve when there are multiple Drone instances.

I want to stop execution of the #Test method if #BeforeMethod is failed. How to achieve This

I go with a general example. I have login code in the #Before method:
#BeforeTest
public void setUp(){
///somelogin logic
}
#Test
public void Testdashboard() throws Exception{
//Some Dashboard validation logic
}
So here if login fails in the #BeforeMethod I want to stop the execution of #Test method how to do this?
I don't think the test method will run if beforemethod fails. The #test method should get skipped automatically.
You can use dependsOnMethods attribute in #Test annotation as below:
#Test(dependsOnMethods={"setUp"})
public void Testdashboard() {
//Some Dashboard validation logic
}
I guess it would not be possible as method annotated with #BeforeMethod is not a test method. It is used to perform any setup prior to execution of test method.
If you want test method to depend on other method then you need to use dependsOnMethods attribute whose value is some other method annotated with #Test.
You can do this by using below option:
-configfailurepolicy- Its value can be skip|continue.
This determines whether TestNG should continue to execute the remaining tests in the suite or skip them if an #Before* method fails. Default behaviour is skip.
Refer to link:
http://testng.org/doc/documentation-main.html

Selenium sample program: Getting Error org.openqa.selenium.WebDriverException: expected expression, got end of script

I am trying to do a sample program with selenium webdriver. I am using libraries from Selenium-java-2.53.1.
Here is my sample program
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
public class ScrollWebPage {
WebDriver driver;
String URL="https://www.gmail.com";
#BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().window().maximize();
}
#Test(priority=1)
public void scrollingToBottom(){
((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");
}
#AfterClass
public void tearDown(){
driver.quit();
}
}
The page is getting opened but it is not able to scroll down. seems an issue with executeScript()
Please help
.executeScript() expecting JavaScript string expression as first arguments while you are providing simply a String as Url which is not an JavaScript expression as exception says, You need to change :-
((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");
to
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
Note :- .executeScript() expect arguments like String arg0, Object... arg1 which means first arguments should be String but it should be JavaScript expression and second arguments should be Array of Object like Object[]
In your case no need to provide URL as arguments if you simply want to execute scrolling function.
Hope it will help you..:)
Simply Use as below to see the scroll working. Try in some other page because gmail don't have a much bigger page to feel the scroll.
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,2500)");

EJB Timer JEE6 does not starts automatically

Why this time does not work? What am I missing?
I'm using GlassFish 3.1.2.
package foo.bar;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
#Singleton
public class MySimpleTimerEJB {
#Schedule(second="*/1")
public void foo() {
System.out.println("Foo");
}
}
Solved,
as Piotr suggested, i have to inform minute and hour aswell, since they have a default zero value.
also i used the persistent=false attribute, and got the expected result.
#Schedule(second="*", minute="*",hour="*", persistent=false)
public void foo() {
System.out.println("Foo");
}
If you want that the scheduling counter starts immediately after the deployement you have to add the #startup annotation so the container will handle and start scheduling management

Resources