How to follow redirect in WebDriver - java

I'm very new to WebDriver, and the whole concept of a headless browser is hard to wrap my head around, and I could use some help...
This might be a stupid Idea for a project in the first place, but I thought I'd give it a shot, so please bear with me.
So I'm using cappex.com as my starting website, and then from there my program will search for GiveAway scholarships. And I have a big ol' set of variables that it will search for then attempt to imput. I'm having a lot of fun with this, the only problem I'm having is when ever it clicks a new scholarship it redirects in a new tab. see linked video for ex.
Is there a way to follow that redirect, like so I can now interact with that website.
there is no method of selenium web diver that I can see for redirects, so I'm not sure how to proceed.
I would share my code, but there's basically nothing yet, and given the nature of the project there is a lot of personal information. I'm programming using Java, in eclipse.
Thank you for your time, and help.

I had the same problem in past so what I did.
Get the WindowHandles
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
Switch to new opened tab
driver.switchTo().window(tabs.get(1));
then do whatever you want.
And if you want to switch to previous tab then
driver.switchTo().window(tabs.get(0));
You can switch to other tabs as well based on the their index.

Related

How to get a web page source code with GeckoView

I am an android developer and I knew about GeckoView recently.
I can get source code of a web page by android WebView (java code).
However with my new website, android Webview can't load the webpage but GeckoView can.
Now I want to get source code of a web page by GeckoView.
Is there any body know the solution to resolve this problem ?
Thank in advance
As far as I know, you cannot use GeckoView to get the source code, but the geckoview library does have GeckoWebExecutor. Take a look at the fetch method and the WebResponse it returns.
By converting the WebResponse.body InputStream to a String you can get the source code.
One option for doing this could follow these steps:
detect which page my geckoview loaded;
save the loaded url into a sharedpref;
if user wants to see the source of the page, load the url saved within that sharedpref.
So...
(1-2) as you already know, geckoview doesn't have that handy shouldOverrideUrlLoading() method, so you will have to deal with the NavigationDelegate class, which has the onLocationChange(), where i put this line, which does nothing more than save the current url as a string into a sharedpref string named "geckoViewUrl":
sharedprefs.edit().putString("geckoViewUrl", url).apply();
having all setup before (sharedprefs etc). Detected the loaded page and saved the url into a sharedpref, let's go to the final step 3.
(3) for my use, wishing only to allow the user to see (and copy) the source, for the law of the minimum effort i used another activity with a plain and old webview to display it. This is very easy to implement and rises no confusion to the user. He/She wants to see the code, i show it in another activity. When it's done, he/she closes the new activity and life continues.
So, user wants source? Load another activity with a webview and make it load that saved sharedpref string:
addr2open = pref_out.getString("geckoViewUrl", "");
Doing this (for example), you get the url user wants to see source assigned to a string var. To finish, all you have to do is to make the webview load this string preceded by the precious word view-source:, this way:
webView.loadUrl("view-source:" + addr2open);
That's it. Of course you could implement a solution relying on the same activity, or using geckoView, showing multiple options, menus etc. I only wanted to show you a way to solve your problem in a nice 'n' easy way. You asked for "How to get a web page source code with GeckoView". Here is the answer. Works perfectly. If this is good for you, please, accept this as the correct answer. Thank you. Happy coding.
One example on the use of "view-source", from the creators of GeckoView: https://firefox-source-docs.mozilla.org/devtools-user/view_source/index.html (see item "Link to a line number").

Vaadin - pass data to different UI

I am new to Vaadin and I tried to find the answer to this by searching SO as well as Google so any help will be appreciated or at least point me in the right direction.
My app navigates from one view to another using the following logic
ActivitiesUI startUpActivity = new ActivitiesUI();
UI.getCurrent().setContent(startUpActivity.buildMainArea());
I need to know how I can pass data (like a String or an int) from the current UI to the one being navigated to (in this case startUpActivity)
Coming from an Android background, I am thinking along the lines of an Intent
After some digging into the Wiki, I ended up using the Http Session to set and get the variables and it is working fine.

How Can we Retain old session of browser using FirefoxProfile?

I was asked a question in an interview:
You are working on a session and there are some questionnaire for first time users. But when the user close the browser and reopen it, the questionnaire must not appear (because he is not a first time user now). I had to tell how to automate this scenario in Selenium using TestNG framework(using Java). So basically he wanted to close/quit the current driver and wanted to use another driver to reopen the page and make sure that new session remembers that the user has visited before (just like its done manually).
My answer to it was to add the cookie to the new driver session. But he dint want to do that. He wanted to test exactly like a user would do.
He gave me a hint about using FirefoxProfile for this to retain the profile that we are working on.
What is the use of FirefoxProfile and how can we use it in current scenario. I'd appreciate if you can provide the code that is to be written for this.
So it comes down to how that specific website determines if you have answered questionnaire or not. As you gave no explanation in the question, I will just assume it is done the way most people do it - that is - using a cookie.
If this assumption is correct, your suggestion to use a cookie is the best suggestion and your interviewers statement to use FF profile simply makes no sense. I mean, I am not even sure you can do such thing with FF profile, anyway, injecting a cookie when a browser is fired (or inject using a proxy) is definitely a way to go with this. It also may or may not be that your interviewer has a little distorted knowledge about how the web works. So:
He wanted to test exactly like a user would do.
how would that really look like in your interviewers mind - "a prankster user wondering around the web, closing his sessions whenever acceptable and starting again with clean installation of a browser, but in the same time always secretly injecting a cookie so he is not getting the questionnaire?"... I am not really a certified expert in this matter and I have no scientific data to prove it, but I hardly think that this is what my users do.. So what users really do is .. nothing. It is the browser that is remembering the cookies, hence giving the webapp a way to understand if this user has visited you or not. So yea, I state again, that injecting that cookie is what you should do. And even if it possible to inject cookies or smth with FF profile - just refuse to do it and use WebDriver object instead, as using a profile for such task just makes no sense (new FF profile is created when FF is launched, also the statement:
"lose/quit the current driver and wanted to use another driver to
reopen the page and make sure that new session remembers that the user has visited before (just like its done manually).
"
: spinning up new WebDriver instance and clicking "open firefox button as a user" are not the same thing. Please consult the docs if you don't believe me. Calling new FirefoxDriver() causes the system to load new FF with new anon profile, whereas the "normal FF installation" lives in the harddisk with all his caches and other stuff.
So sometimes in interviews they intentionally give you questions that make no sense and trying to solve them would hurt your family as well as mine and all the mankind in general - like "Write a regexp to parse HTML". In these cases you just need to say: "Thats not really how we do it, let proceed to the next question". And thats what you should do in my opinion. Best luck.

Trasparent intro overlay to introduce user the app

I was wondering to create something like a setup wizard for when the user first starts my app. This is needed due to the complexity of the app to help the user. Searching for something like this I found a library that isn't a setup wizard, but lets you point an element on the screen and give some info about it.
The library is this link. (Showcase View Library by Espin)
I'm able to show one indication using this, but I can't concatenate more than one indication, you know, the first is shown, you pulse next and goes to the nex indication, this way until you arrive to the end and pulse finish.
Looking for any tutorial or step-by-step guide that could help me doing this, I found one, but it was done with old code, and the newest version of the library has some changes that doesn't fit the example. I've tried modifiying the code of this example to match with the new version of the library, but I don't get to view the indication one by one, I just get all them overlapped.
This is the link to the library's ShowcaseView class where all this logic is defined: ShowcaseView.java
And this is how the doc says to implement it ot your app:
new ShowcaseView.Builder(this)
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the Home button")
.hideOnTouchOutside()
.build();
Has someone worked with this library and knows how could I concatenate few indications?
What's missing in v5
ShowcaseViews: the class which queues up ShowcaseViews in a
tutorial-type method. I never really liked this class (generally, you
should use SCV sparingly); I'll add it back in based on the Builder
class when I can.
So either:
implement it by yourself and submit a pull request.
Or wait for the next release.
Or use v4
Or submit an issue on github to directly ask the author when this will be released.

How can I add the functionality of User Input to my eclipse plugin?

I am developing a plugin in eclipse which can generate a text file (based on user input) and can trigger a perl script. I can easily make a simple plugin (like Hello World) and I can add new menus and commands to it. I understand how those things are related to one another, but the place where I am getting stuck is the User Input.
How can I ask the user to enter his choices? I mean what extension point should I use to ask him out? I can't find anything that can ask the user to enter data. Once I can get him to enter the required info, I can easily access that information to proceed further.
PLZ. NOTE: this is my first time in an online community, so I have tried to be as thorough as possible in my description. I am new to eclipse and have a very basic knowledge of Java. I took some Java lessons online by Mark Dexter. I do have a solid understanding and work experience in C++.
Continued from my comment above (sorry, couldn't fit into the 600 character limit for comments, so I'm adding this as a possible answer to your question) -- A quick search led me to this - http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html. You can use actions to easily add toolbar buttons, menu items, etc, that are handled by your plugin. In short, this is how you plug functionality into the workbench. I recommend checking the link above to gain a better understanding. As for collecting the input, you can use a JFace dialog (http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjface%2Fdialogs%2FDialog.html). There are many pre-canned dialog that you may find useful, so I recommend doing a little more research to see what will work best for your use case. Good luck!
Starting Eclipse plug-in development might be a bit overwhelming.
The sort answer:
I assume that you have create a new plug-in project with the "hello, world command" template. If not you can easily do it. It will help you to get started. In this sample project, eclipse created a simple menu contribution for you. I'm not going to go over the details, but essentially, you create a command (org.eclipse.ui.commands) and associate it with some location in the workbench like a menu or toolbar icon (org.eclipse.ui.menus). Then you need to create something that will handle this command like when it is executed (by user clicking on the menu). This is done by by handlers (org.eclipse.ui.handlers) and handlers are when you want your code to go into.
Open the "plugin.xml" file in your plug-in and navigate to the "Extension" section
Expand the "org.eclipse.ui.handlers" branch and select the only "(handler)" item.
Click on the "class" link the right part of the editor and that will navigate you to the generated class.
Your code goes to the "execute" method. This is what will be run when user executes the command. In your case if you want to ask for an input you can use an input dialog:
Code:
public Object execute(ExecutionEvent event) throws ExecutionException {
InputDialog dlg = new InputDialog(
HandlerUtil.getActiveShellChecked(event), "Title",
"Enter text", "Initial value", null);
if (dlg.open() == Window.OK) {
// User clicked OK; run perl
String input = dlg.getValue();
// TODO:do something with value
}
return null;
}
No to ask user for an input you will need to show a dialog. I suggest you go over this nice tutorial: http://www.vogella.de/articles/EclipseDialogs/article.html which describes this in a great detail.
The long answer is to look at http://www.vogella.de/articles/EclipsePlugIn/article.html and http://www.vogella.de/articles/EclipseCommands/article.html to see the basics behind Eclipse RPC and commands.
Lastly, you will also need to look in here http://www.vogella.de/articles/EclipseJobs/article.html to understand how to run longer tasks like the perl script you are talking about.
There are no extension points in Eclipse used to input information from the user. For this you typically use a dialog or a view depending on the specific use case.

Categories

Resources