What is example of method overriding in selenium webdriver? - selenium-webdriver

As I went through tutorial, all written about get() method is example of method overriding.
But as per method overriding method which is present in parent class is having different implementation in child class.
And get() method is declared webdriver interface and implemented in RemoteWebDriver interface.
Then how overriding is achieved here?
Could you please help with query.
Any other example with method overriding in selenium webdriver please share?

If you are asking specifically for the get() function, here is the implementation tree:
public class ChromeDriver extends RemoteWebDriver
implements LocationContext, WebStorage, HasTouchScreen, NetworkConnection {...}
Here chromeDriver is an example class can be any other. It extends the RemoteDriver and implements a bunch of other interfaces. And the RemoteDriver Class extends and implements some more as below
public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
FindsById, FindsByClassName, FindsByLinkText, FindsByName,
FindsByCssSelector, FindsByTagName, FindsByXPath,
HasInputDevices, HasCapabilities, Interactive, TakesScreenshot {
...
public void get(String url) {
execute(DriverCommand.GET, ImmutableMap.of("url", url));
...
}
As you can see the RemoteDriver is overrridng the get() method it gets from the WebDriver interface
public interface WebDriver extends SearchContext {
....
void get(String url);
....
}

This is a simple overriding example of the "findElement" method.
public static WebElement findElement(By Locator){
WebElement anElement = fluentWait.until(new Function<WebDriver, WebElement>() {
#Override
public WebElement apply(WebDriver webDriver) {
webDriver=sampleDriver;
return sampleDriver.findElement(Locator);
}
});
return anElement;
}

Overriding something we give different implementation in child class . And override method which was in parent.

Related

Simple Camel test fails with no messages recieved

Am using Spring Boot and I have just added camel to it.
I have a simple camel route setup :
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
#Component
public class MyRoute extends RouteBuilder {
#Override
public void configure() throws Exception {
from("file://in").to("file://out");
}
}
When I try to create simple test for this route with :
#RunWith(CamelSpringBootRunner.class)
#SpringBootTest
#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class MyRouteTest extends CamelTestSupport {
#Autowired
private CamelContext camelContext;
#Produce(uri = "file://in")
private ProducerTemplate producerTemplate;
#EndpointInject(uri = "mock:file://out")
private MockEndpoint mockEndpoint;
#Test
public void routeTest() throws Exception {
mockEndpoint.expectedMessageCount(1);
producerTemplate.sendBody("Test");
mockEndpoint.assertIsSatisfied();
}
}
It fails with
mock://file://out Received message count. Expected: <1> but was: <0>
Not sure what could be a problem here. I have producer template that has uri as my route from point and am mocking to endpoint with EndpointInject and the the mock uri?
Fixed but not 100%
If I change route from real one
from("file://in").to("file://out");
to
from("file://in").to("mock:out");
And in my test override
#Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new MyRoute();
}
to create specific route
and strangest of all ! Had to remove :
#SpringBootTest
and after that
private CamelContext camelContext;
And then it started working !
But unfortunately not what I need, still there are things that need to be fixed, I would like to use my real prod route !
from("file://in").to("file://out");
And if possible not use advise on route , but just mock it , tried with
mock:file://out in test, but it didnt work :(
and also , it does not work with #SpringBootTest ??? very strange ?!
You need to add
#Override
public String isMockEndpoints() {
return "*";
}
This should mock all the enpoints and then you can use mock:file:out for example
If I am not misstaken you are mocking your output endpoint yet your endpoint endpoint is a file endpoint. When you send a message you need to drop a message to whereever the file endpoint is polling. Otherwise you need to mock that as well.

C# + WebDriver PageFactory + Nunit 3 Parallelizable

I have implemented the following setup in my automation framework:
C# Selenium webDriver
IObjectContainer
Specflow
Nunit 3 Parallelizable
Xpaths (and all selectors) are simply defined as 'private const string'
code sample HomePageSteps (working code):
[Binding]
public class HomePageSteps
{
private IWebDriver _driver;
public HomePageSteps(IWebDriver driver)
{
_driver = driver;
}
private const string LogoTwo = "//img[#alt='xpath']";
[Given(#"I navigation to site")]
public void GivenINavigationToSite()
{
_driver.Navigate().GoToUrl("http://website.com/");
_driver.FindElement(By.XPath(LogoTwo)).Click();
}
}
But I want to use the webDriver PageFactory attribute to handle my page objects. Which I have done / used in other projects that do Not use 'Nunit 3 Parallelizable'.
code example (NOT working code):
[Binding]
public class HomePageSteps
{
private IWebDriver _driver;
public HomePageSteps(IWebDriver driver)
{
_driver = driver;
}
[FindsBy(How = How.XPath, Using = "//img[#alt='xpath']")]
public IWebElement logo { get; set; }
[Given(#"I navigation to site")]
public void GivenINavigationToSite()
{
_driver.Navigate().GoToUrl("http://website.com/");
logo.Click();
}
}
The problem here is that 'logo' is Null. And I'm not sure if this problem is related to a limitation of use ''Nunit 3 Parallelizable' with PageFactory, or with specflow, or something else in my design.
Can anyone please give some guidance?
The reason Page Factory would not initialize non-static driver is that it takes in Static driver. If The constructor for the page is defined like this :
ChromeDriverService service = ChromeDriverService.CreateDefaultService(startDirectory + #"\Drivers\", "chromedriver.exe");
IWebDriver Static driver = new ChromeDriver(Service);
public HomePageSteps()
{
PageFactory.InitElements(driver, this);
}
It would allow you to use the elements as you have defined them.
Please let me know if you have found any other work around to this.

How to use properties class object in child class

I have created a class 'a' with in package name 'pack1'
#Beforetest
public class a {
public properties prop;
public propLoad(){
Webdriver driver= new firefoxdriver();
prop = new properties();
prop.load(driver);
}
also i have created a new package 'pack2' and new class 'b' and i want to use the prop object in b class
i have written the code as
#Test
public class b extends a{
prop.getproperties(keyname);
}
(Keys and values are defined in .properties file)
But when i have ran the code system gives me null pointer exception
Pls help me in this issue
Define prop = new properties(); at class level
#Beforetest
public class a {
public properties prop=new properties();
public propLoad(){
Webdriver driver= new firefoxdriver();
prop.load(driver);
}
I am not sure for what reason you need to pass driver object here? prop.load(driver); Anyways, instantiating prop class object at class level should do a trick. Let me know.

Selenium Webdriver_Java_How to Call Mutiple Class into One Class

I stuck at point i have one website for example (http://newtours.demoaut.com/) and for that i have created multiple classes likes:
App_Login(),
App_Dashboard(),
App_FliReservation(),
Cancel_Flight();
So want to call above all classes into single class .
can someone guide me on these.
Script sample
App_Login() class
public class App_login {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver= new FirefoxDriver();
driver.get("http://newtours.demoaut.com/");
}
}
and i want to call above class into following class
public class Mercurywebapp {
#Test
public void supp_onboarding() throws Exception {
App_Login();
}
public void App_Login() {
// TODO Auto-generated method stub
}
}
when i run above class (i.e.Mercurywebapp) then getting blank screen
Create another instances of all the Classes in the MercuryWebapp
Depends on the access modifiers for the classes
e.g
App_Login testObject = new App_Login();

Intercepting annotated methods using Spring #Configuration and MethodInterceptor

I need to intercept annotated methods using spring-aop.
I already have the interceptor, it implements MethodInterceptor from AOP Alliance.
Here is the code:
#Configuration
public class MyConfiguration {
// ...
#Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
}
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface MyAnnotation {
// ...
}
public class MyInterceptor implements MethodInterceptor {
// ...
#Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
//does some stuff
}
}
From what I've been reading it used to be that I could use a #SpringAdvice annotation to specify when the interceptor should intercept something, but that no longer exists.
Can anyone help me?
Thanks a lot!
Lucas
MethodInterceptor can be invoked by registering a Advisor bean as shown below.
#Configurable
#ComponentScan("com.package.to.scan")
public class AopAllianceApplicationContext {
#Bean
public Advisor advisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("#annotation(com.package.annotation.MyAnnotation)");
return new DefaultPointcutAdvisor(pointcut, new MyInterceptor());
}
}
In case anyone is interested in this... apparently this can't be done.
In order to use Java solely (and no XML class) you need to use AspectJ and Spring with #aspect annotations.
This is how the code ended up:
#Aspect
public class MyInterceptor {
#Pointcut(value = "execution(* *(..))")
public void anyMethod() {
// Pointcut for intercepting ANY method.
}
#Around("anyMethod() && #annotation(myAnnotation)")
public Object invoke(final ProceedingJoinPoint pjp, final MyAnnotation myAnnotation) throws Throwable {
//does some stuff
...
}
}
If anyone else finds out something different please feel free to post it!
Regards,
Lucas

Resources