Is TestNG's ITestResult Interface or a Class? if interface which class implemented this interface? - selenium

Is TestNG's "ITestResult" an Interface or a Class? we are using the below code to take the screenshot of the webpage whenever there is a failure. we are getting the status of the test by using ITestResult. I have checked the TestNG's API and it is showing as ITestResult as a interface, then which class is implemented this interface? And how we are accessing the method getStatus() using Interface reference? Can anyone clarify my doubt?
#AfterMethod(alwaysRun=true)
public void takeScreenShot(ITestResult result){
if(result.getStatus()==2){
ITestNGMethod instance = result.getMethod();
String testName = instance.getMethodName();
TestUtil.captureScreen("xyz", testName,"Failure_Screenshots");--> method to take screen shot and save the image file in specific directory
}
}
Thanks in Advance!!

org.testng.internal.TestResult is the class which implements the ITestResult interface.
In eclipse, you can click on a type and click Ctrl+T to see the Type Hierarchy to get this data for any type.

Related

how to transform interface to abstract by using javassist

I am using javassist library for modify class files.
I want to modify interface to abstract class
for example,
original :
public interface javax.servlet.Servlet {
public void init(ServletConfig config) throws ServletException;
}
modified :
public abstract javax.servlet.Servlet {
public void init(ServletConfig config) throws ServletException {
System.out.println(config.getServletContext().getServerInfo());
callMethod(); // this is implemented original method
}
}
How can i apply this solution like aop(before, after)?
I think the first problem with your approach is that when you try to modify your interface using Javassist you are attempting to redefine an interface that has been already loaded by the class loader.
One option might be to do a bit of classloader tricks: create a new classloader that doesn't have your existing interface loaded (parent is the system classloader) and have Javassist load through that (use aCtClass.toClass() method that takes a ClassLoader argument). However is not really something I would do since to manage properly more than one ClassLoader is not that easy.
There might be a better way to achieve your goal, and creating new classes might be a better design. You could create a new class that implements everything you need and then extends the required interface.
Moreover I suggest you to take also a look at dynamic proxies that could be an option as well. Their biggest advantage is that you don't need any 3rd party libraries to create them.

intelij idea shortcut to implement interface as a new class

suppose i have a interface like this:
public interface Dumb{
String getDumbName();
}
Is there any shortcut or menu in intellij-idea to create new classes implementing the interface with dummy implement methods like this:
public class Dumber{
public String getDumbName(){
return null;
}
}
There are multiple ways to go about this.
On the interface name itself, you can hit Alt+Enter (Option+Enter on Mac), then pick 'Implement interface'. IDEA will prompt for a class name and a package to put the new class in, then generate an implementation class.
Alternatively, create the class, then add implements Dumb after the name (im<tab> Dumb). IDEA will complain that your class doesn't implement the correct methods, and offer (Alt+Enter Enter Enter) to generate them for you. Hitting Ctrl+I or clicking 'Implement methods' in the Code menu also works.

get Annotation of test method in testNG ITestListener

I am trying to integrate TestLink with TestNG
Approach is below
1>Write ITestListner with onTestFailure and onTestSuccess
2> get Annotation of the method(like testName which will be equivalent to test name in testlink) which is being failed/success in a variable
3>Make connection with TestLink using API available and update the test case.
However I am struggling to find method Annotation value in ITestListner and requirement is to get annotation values in ITestListner only so that correct test cases can be updated in Test_link
Can someone please help me how to get Test Method annotation value in ITestListner or any other approach in which i can integrate testlink update with TestNG
Hi Thanks niharika for help
,First of all you are correct in explaining use of TestNG but we are using TestNG for Selenium and already there are around 1000 test cases writen in test Methods and we have to live with that
Some how i have figured the solution ,we can still get the testName of the test method using two listners
This is just work around I am not sure if this is the best approach but as of now solving my purpose
package com.automation.testng.listner;
import org.testng.*;
public class MyIInvokeMethodListner_TestName_TestLink implements IInvokedMethodListener {
public static String testName;
public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
// TODO Auto-generated method stub
}
public void beforeInvocation(IInvokedMethod m, ITestResult tr) {
// TODO Auto-generated method stub
//This give the Annotation Test object
org.testng.annotations.Test t=m.getTestMethod().getMethod().getAnnotation(org.testng.annotations.Test.class);
MyIInvokeMethodListner_TestName_TestLink.testName = t.testName().toString();
}
}
MyITestListner goes like below
package com.automation.testng.listner;
import org.testng.*;
public class MyITestListner_TestLink extends TestListenerAdapter {
/*IAnnotationTransformer at;
public Listner_1()
{
this.at = new Annotation_listner();
}*/
#Override
public void onTestFailure(ITestResult tr)
{
System.out.println("Hurray !I am being inboked from Test listner");
MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}
public void onTestSuccess(ITestResult tr)
{
MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}
}
Basically we are getting the method and then using Test Annotation class setting the static variable which can be used in MyITestListner
The ITestListener is the one which is used after <test> tag. For getting the method name and annotation specifics, you need to implement IInvokedMethodListener and in the after/before methods of this interface, and use something like method.getTestMethod().getMethodName() to get the executing method name.
If you are adding testName at the method level, I think you are doing it wrong since the help of testng mentions this "The name of the test this test class should be placed in. This attribute is ignore if #Test is not at the class level."
If you are indeed specifying the #Test at your class level then you can get it as below :
method.getTestMethod().getTestClass().getTestName()
A bit ugly and you probably want to wrap those parts in null checks in your code but this is how you get the testName specified in the annotation from the ITestResult:
iTestResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class).testName()

Implementing the Page Object Model

I am building a test automation project using Perl and Selenium. I am using the Page Object Model. I am somewhat unsure about where the Selenium driver would fit into the implementation of the page object model.
Should each page object 'have' a driver ? The way I am thinking is that each page object represents a set of services that the page offers to a user. With this concept in mind , a page object does not have a 'has-a' relationship with a driver. A page object interacts with a driver. However, I am still looking for suggestions. Should I have the driver as part of each page object in the web-application?
Thanks!
This answer won't be any much different from #zodvik, and your thought process, but is another optional approach. Instead of passing the driver around, you can create an abstract class that each page object can then inherit from. From the abstract class, can also contain some common functional methods that you will find yourself often using.
This is at least how I do it in Java language.
I always include driver as part of every Page Object. The way I thought about driver was that it represents the state of the current page. It gives access to the URL, Page Source, etc.
Now, each page has a a current URL, a page source code, a page title which are all accessible through the driver.
In the way, I implemented the framework. I used the driver in the commonFactory.class that contains commonly used elements. Each page was implemented as a child class of the commonFactory.class. so you don't have to implement the driver in each and every class. Since the driver is independent of the test scenarios it's better to have it in a separate way.
As I understand, there are No set rules/standards for implementing POM.
However, the general thumb rule is to create a BaseTest and BasePage class in your Framework where each respective webpage (like Login) will represent by its PageClass(LoginPage).
Similarly, all your Page classes will extend your BasePage and all tests will extend your BaseTest class.
Below is a rough idea of its implementation ->
public class BaseTest{
#BeforeSuite()
setupMethod(){
initialize your WebDriver here
}
}
------------------------------------------
public class BasePage {
//create constructor
public BasePage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(this.driver, Duration.ofSeconds(TIMEOUT));
PageFactory.initElements(new AjaxElementLocatorFactory(this.driver, TIMEOUT), this);
}
//other common methods which can be utilized in your respective child Page classes
}
----------------------------------------------------
public class LoginPage extends BasePage {
//Your Locators and Weblelements
private static final LOGIN_ID = "login";
//Constructor to supply webdriver
public LoginPage(WebDriver driver) {
super(driver);
}
//your action methods
public void loginToApp(){
driver.findbyelement(By.ID(LOGIN_ID)).click
}
}
----------------------------------------------------
public class LoginTest extends BaseTest{
public LoginPage login;
BeforeAll()
{
login = new LoginPage(driver);
}
#Test
public void verifyLogin(){
login.loginToApp();
}
}

How can I pass parameters from Shell to Modules through constructors in Silverlight project which using Prism 4.0 for integration

I have a main Silverlight Shell project, which calls several Silverlight Module projects.
I need to pass parameters to my module projects through constructors.
Can anybody help me to solve this?
"Ask and yea shall receive" is the IOC motto :)
Prism uses injection via the UnityContainer. When a module is loaded it will resolve any registered interfaces specified in the constructor of the module.
Just specify an interface to an object that you have previously registered as a singleton and it will be passed to with any module. Place all your settings/parameters in that singleton.
If you need more information, just ask.
Register an object with the container.
class MyBootStrapper : UnityBootstrapper
{
protected override void ConfigureContainer()
{
base.ConfigureContainer();
this.Container.RegisterInstance(typeof(IMyInterface), new MyInterfaceImpl());
}
}
Now, the module constructor happily receives that object.
class ContentModule : IModule
{
private readonly IMyInterface _myInterfaceImpl;
public ContentModule(IMyInterface myInterfaceImpl)
{
_myInterfaceImpl = myInterfaceImpl;
}
#region IModule Members
//
#endregion
}
Courtesy: TrueBlueAussie