Selenium control multiple browsers at the same time - java

I'm still learning to use Selenium and come up with this question, supposed that I want to open 2 same browsers (2x firfox) at the same time, for the first browser I want it to navigate to google.com and search for the first keyword then click on the first link. In the mean time, the second browser will navigative to google.com and search for the second keyword then click on the first link. Those actions are happening at the same time so they will (or may) close at the same time to save time.
Can you please point me to the right direction what do i need to read or look into in order to archive this task? I read another post on Swing Worker but that's another multi-threading thing. My naive approach is to create a new driver then start them at the same time, but is it any other ways?
Thank you.

With the help of maven and TestNG this can be accomplished. Check this answer and git maven template

Kindly refer below to run parallel browser testing using TESTNG :
TestNG - Parallel browser testing
You will also get best tutorial for selenium web driver and testNG at Selenium Web driver tutorial

Yes with multithreadingyou can achieve this. A simple way can be to create multiple threads of each testcase in your launcher file and each testcase can create its own web driver object and maintain it. Just implement thread in your test case and off you go. And yes after that you can create a jar and run them on different machines.

Related

How can we make use of Selenium WebDriver and Appium in the same scenario implemented using Cucumber?

I have a situation where I need to perform some steps in the Website(Selenium) where I execute some steps to create some data and them it is pushed to mobile app. I want to work on it on the Mobile side(Appium) and take the flow ahead. Once I complete the operations on mobile. I must validate the same on Web site again.
This all needs to be done in one scenario as I don't want my scenarios to be dependent on other scenario.
Basically the web flow will be present in every scenario I would be writing.
Now when I am trying to do this using Background or #Before, Even my Appium object is instantiated and remains idle till the time WebDriver performs its steps. I want my AppiumDriver to be instantiated only after the execution of WebDriver Steps. I appreciate your help. TIA!
background once in a feature before any scenario on that feature.
before executes each time before any scenario.
As per your requirement it depends when you are initializing your AppiumDriver instance, It means if your initializing your appium driver in a gherkin statement then using before can work for you, use
Tagged Hooks
It is possible to define a hook so that it is only executed before or after scenarios that are tagged with a specified annotation, for example:
Feature: web text munger kata
#web
Scenario: It should process a sentence
Tag your scenario which required this before to run only for it, not for all
Stepsdefination:
#Before("#web")
public void beforeScenario() {
// perform your webdriver code
}
For better understanding go through the below link:
https://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/

Jsoup: Getting a link that doesn't show in the HTML

I am working on a little app for myself. I am trying to get a list of links from a site. The site is for example: http://kinox.to/Stream/Prison_Break.html
If you hover over the big window in the middle that says kinox.to best online, it show the link that I want in the bottom left. The problem is if I look at the html file I can't find the link anywhere. I guess it has to do something with the site using JavaScript or Ajax.
Is it possible to somehow get the link using JSoup or are there any other Java libraries that could help me?
I did not look closely into the page you try to load, but here is what I think the problem may be: The link is loaded/generated dynamically via JavaScript. Jsoup does not run JavaScript, so therefore you can't find the link in the html.
Two possible solutions:
1) Use something like selenium webdriver to access the content. The Java bindings allow to remote control a real browser which should have no problems loading the page and running all scripts within. Solution 1 is simple to program, but runs slowly. It may depend on an extern browser program which must be installed on the machine. An alternative to webdriver is the JavaFx webkit engine in case you are on java 8.
2) Analyse the traffic and the JavaScript on the page and find out where the link comes from. This may take a bit of time to find out, but when you succeed you can use Jsoup to get all the data you need. This solution should run much faster than solution 1.
One solution and probably the easiest would be to use Selenium:
WebDriver driver = new FirefoxDriver();
driver.get("http://kinox.to/Stream/Prison_Break.html");
String mylink = driver.findElement(By.cssSelector("#AjaxStream > a")).getText();

Using the same Firefox Window to run multiple tests in Selenium WebDriver (Java)

I am running test cases on selenium Webdriver in Java. The first test case opens the browser window and performs the test.
After the first test case is complete I want to utilize the same browser window to run the next set of multiple test cases.
How can this be achieved? Can someone point me in right direction?
Don't quit your browser in the "TearDown" part.
Navigate to a common URL in the "TearDown" part from where multiple test cases can start.
Thus you will be able to utilize the same browser window to run the next set of multiple test cases.
You can reuse a browser in multiple tests using Spring to inject it. This can be faster for running a suite. But, and it's a big but, if one tests "dirties" the browser (e.g. with cookies), then you could easily find yourself spending more time debugging flaky tests that you save on run time.
There's an example here: http://www.alexecollins.com/tutorial-integration-testing-selenium-part-1/

How to select proper Action for web target in Selenium IDE

I am new to Selenium IDE. As far as I know, when open Selenium IDE, you will notice that the red 'record macro' button is toggled. This means that selenium will attempt to record every action you make inside the browser. This is a problematic way of recording as we implicitly wait for actions to complete before moving on.
If I only let Selenium to record every actions without specifying extra actions, many test step will be failed with error message : Element not found. I was trying to add extra actions based on Selenium API, like waitForElementPresent, waitForSearch etc.
My question is: How do I know which extra action do I need to add for each web target? Any standard for it? Thanks!
I use webdriver but I am familiar with IDE and so far I know it depends on your application how you want to handle the tests. If your application uses ajax calls you might need to use some frequent waitForElementPresent or waitForSearch etc.. and Assertions also depend on the needs of your tests.
Now, the question is how do you know which extra step do you need to insert?
Ans. is you will know the necessity. Such as, if your test step depends on a previous ajax call to finish then you know there is a wait necessary and you know what to do. Not to mention, you can always insert extra steps and I am sure you already know that. And, there is no standard for using those. You adjust your tests depending on your necessity
you need to go through introduction to selenium ide or just think straight this way that if any action needs loading of page, you simple need to wait for element present and the perform click
click|target|
waitForElementPresent|target|
or if you need to store any value you can use
storeEval|target|value
also the variable name in the selenium ide is named followed by $variableName
enter can be performed as ${KEY_ENTER}
to verify any value we can use AssertValue or VerifyValue
the difference between assert and verify is that assert stops the execution of test case if the value is false whereas verify gives error and execute next statement.
these are few points to be noted in selenium ide.
hope this answer would help you!
You may want to try Implicit Wait addon for Selenium IDE. It will automatically call WaitForElementPresent before executing actions on that element (like clicks). This may save you some time.
Here is the link of Selenium API, all actions can be found here.

How to code an automated bot that can browse and do operations on a webpage. JAVA

need to code a bot that needs to do the following:
Go to a jsp page and search for something by:
writing something on a search box
clicking the search button(submit button)
clicking one of the the resulting buttons/links(same jsp page
with different output)
get the entire html of the new page(same jsp page with different
output)
The 4th one can be done with screen scraping and I do not think I need help with it. But I need some guidance to do the options from 1 to 3. Any links or just some keyword that will help me Google to learn about it will be appreciated. I plan to do this with java.
My suggestion is to use Selenium (http://docs.seleniumhq.org/download/).
Install Selenium IDE in your firefox, and it can record what you do on a website, store it into a script and reply it.
This video (http://www.youtube.com/watch?v=gsHyDIyA3dg) is gonna be helpful if you are a beginner.
And if you want to do it in Java, its easy, just export the scripts in Selenium IDE to JUnit Webdriver code.
Of course you can use Selenium Java webdriver in Java to write your program to operate on website directly.
Selenium automates browsers. That's it. What you do with that power is entirely up to you.
The above steps can be done by using selenium(which is a testing tool in java)
Even points 1 to 3 are screenscraping - you're figuring out (using either manual or automated means) what's there in the page and performing actions on them. You could try exploring the Apache HTTP Client for an easy way to run HTTP commands and get responses.
I hope you're doing this for legitimate means - screenscraping is almost always frowned upon if done without permission.

Categories