In Selenium Webdriver how we can skip the data download? - selenium-webdriver

I want to skip the loading of whole web page and just want to check status of URL in selenium webdriver so that the execution can be much faster.

If you just want to check status of a page and don't need to bother about content inside that, you can just get response code and check if it is 200(HTTP_OK). I would suggest you use simple java to verify instead of trying to get it done with selenium webdriver. I just wrote a small program that does just that. See it this works fine for you. This tests if the web page can be successfully reached and didn't send back any 404 or 500 or any other error.
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest
{
public static void main(String[] args) throws IOException
{
URL url = new URL ( "http://www.google.com/");
HttpURLConnection conn = ( HttpURLConnection )url.openConnection ();
conn.setRequestMethod ("HEAD"); // GET will fetch the content of the page, which is not needed for your case.
conn.connect () ;
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK)
System.out.println("Success");
}
}

What do you want to do? Do you want to click some links on a page and check whether they are redirecting to right URL? In this case you can try checking for change in URL. See the example below:
int i=0;
do{
try {
Thread.sleep(20);
} catch (InterruptedException e) {
}
i++;
} while(driver.getCurrentUrl().equals("The URL on which the page is originally.")&&i<10); //After URL changes, verify it.

Related

How to integrate Selenium Webdriver with Cucumber

I am looking to integrate Selenium Webdriver with Cucumber. Please let me know if anyone has already implemented the same.
You need the Cucumber-JVM. After that when you write feature files you need to define their steps definitions using Selenium (this could be your POM Based project or direct api call).
Below are links on how to go about implementing this
Links
http://cukes.info/install-cucumber-jvm.html
http://cukes.info/running.html
Snippets
Feature Sample
Feature: Page Verification
Scenario: Verify Page
Given User navigated to Page
And user SignUp
Then user should be redirected to Login
When User click "https://page.com/userLogin/"
Then User Should verify "Login"
And close browsers
Steps Definition
public class StepsDefinitions {
WebDriver driver = null;
#Given("^User navigated to \"([^\"]*)\"$")
public void gotoSite(String link) throws Throwable {
driver = new FirefoxDriver();
driver.navigate().to(link);
}
#When("^User click \"([^\"]*)\"$")
public void clickSignUp(String link) throws Throwable {
driver.findElement(By.linkText(link)).click();
}
#Then("^User Should verify \"([^\"]*)\"$")
public void User_Should_verify(String title) {
Assert.assertTrue( driver.getTitle().equals(title));
}
#And("^close browsers$")
public void close_browsers() throws Throwable {
driver.close();
}
I would recommend you to go through the detailed quick set-up instructions in the link.
Resource: Blog Thomas Sundberg

How to handle login pop up window using Selenium WebDriver?

How to handle the login pop up window using Selenium Webdriver? I have attached the sample screen here. How can I enter/input Username and Password to this login pop up/alert window?
Thanks & Regards,
Use the approach where you send username and password in URL Request:
http://username:password#the-site.com
So just to make it more clear. The username is username password is password and the rest is usual URL of your test web
Works for me without needing any tweaks.
Sample Java code:
public static final String TEST_ENVIRONMENT = "the-site.com";
private WebDriver driver;
public void login(String uname, String pwd){
String URL = "http://" + uname + ":" + pwd + "#" + TEST_ENVIRONMENT;
driver.get(URL);
}
#Test
public void testLogin(){
driver = new FirefoxDriver();
login("Pavel", "UltraSecretPassword");
//Assert...
}
This should works with windows server 2012 and IE.
var alert = driver.SwitchTo().Alert();
alert.SetAuthenticationCredentials("username", "password");
alert.Accept();
This is very simple in WebDriver 3.0(As of now it is in Beta).
Alert alert = driver.switchTo().alert() ;
alert.authenticateUsing(new UserAndPassword(_user_name,_password));
driver.switchTo().defaultContent() ;
Hopefully this helps.
Now in 2020 Selenium 4 supports authenticating using Basic and Digest auth . Its using the CDP and currently only supports chromium-derived browsers
Example :
Java Example :
Webdriver driver = new ChromeDriver();
((HasAuthentication) driver).register(UsernameAndPassword.of("username", "pass"));
driver.get("http://sitewithauth");
Note : In Alpha-7 there is bug where it send username for both user/password. Need to wait for next release of selenium version as fix is available in trunk https://github.com/SeleniumHQ/selenium/commit/4917444886ba16a033a81a2a9676c9267c472894
Solution: Windows active directory authentication using Thread and Robot
I used Java Thread and Robot with Selenium webdriver to automate windows active directory authentication process of our website.
This logic worked fine in Firefox and Chrome but it didn't work in IE. For some reason IE kills the webdriver when authentication window pops up whereas Chrome and Firefox prevents the web driver from getting killed. I didn't try in other web browser such as Safari.
//...
//Note: this logic works in Chrome and Firefox. It did not work in IE and I did not try Safari.
//...
//import relevant packages here
public class TestDemo {
static WebDriver driver;
public static void main(String[] args) {
//setup web driver
System.setProperty("webdriver.chrome.driver", "path to your chromedriver.exe");
driver = new ChromeDriver();
//create new thread for interaction with windows authentication window
(new Thread(new LoginWindow())).start();
//open your url. this will prompt you for windows authentication
driver.get("your url");
//add test scripts below ...
driver.findElement(By.linkText("Home")).click();
//.....
//.....
}
//inner class for Login thread
public class LoginWindow implements Runnable {
#Override
public void run() {
try {
login();
} catch (Exception ex) {
System.out.println("Error in Login Thread: " + ex.getMessage());
}
}
public void login() throws Exception {
//wait - increase this wait period if required
Thread.sleep(5000);
//create robot for keyboard operations
Robot rb = new Robot();
//Enter user name by ctrl-v
StringSelection username = new StringSelection("username");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//tab to password entry field
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(2000);
//Enter password by ctrl-v
StringSelection pwd = new StringSelection("password");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//press enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
//wait
Thread.sleep(5000);
}
}
}
The easiest way to handle the Authentication Pop up is to enter the Credentials in Url Itself. For Example, I have Credentials like Username: admin and Password: admin:
WebDriver driver = new ChromeDriver();
driver.get("https://admin:admin#your website url");
This is a solution for Python based selenium, after going through the source code (here).
I found this 3 steps as useful.
obj = driver.switch_to.alert
obj.send_keys(keysToSend="username\ue004password")
obj.accept()
Here \ue004 is the value for TAB which you can find in Keys class in the source code.
I guess the same approach can be used in JAVA as well but not sure.
My usecase is:
Navigate to webapp.
Webapp detects I am not logged in, and redirects to an SSO site - different server!
SSO site (maybe on Jenkins) detects I am not logged into AD, and shows a login popup.
After you enter credentials, you are redirected back to webapp.
I am on later versions of Selenium 3, and the login popup is not detected with driver.switchTo().alert(); - results in NoAlertPresentException.
Just providing username:password in the URL is not propagated from step 1 to 2 above.
My workaround:
import org.apache.http.client.utils.URIBuilder;
driver.get(...webapp_location...);
wait.until(ExpectedConditions.urlContains(...sso_server...));
URIBuilder uri = null;
try {
uri = new URIBuilder(driver.getCurrentUrl());
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
uri.setUserInfo(username, password);
driver.navigate().to(uri.toString());
You can use this Autoit script to handle the login popup:
WinWaitActive("Authentication Required","","10")
If WinExists("Authentication Required") Then
Send("username{TAB}")
Send("Password{Enter}")
EndIf'
I was getting windows security alert whenever my application was opening. to resolve this issue i used following procedure
import org.openqa.selenium.security.UserAndPassword;
UserAndPassword UP = new UserAndPassword("userName","Password");
driver.switchTo().alert().authenticateUsing(UP);
this resolved my issue of logging into application. I hope this might help who are all looking for authenticating windows security alert.
Simply switch to alert and use authenticateUsing to set usename and password and then comeback to parent window
Alert Windowalert = driver.switchTo().alert() ;
Windowalert.authenticateUsing(new UserAndPassword(_user_name,_password));
driver.switchTo().defaultContent() ;
1 way to handle this you can provide login details with url. e.g. if your url is "http://localhost:4040" and it's asking "Username" and "Password" on alert prompt message then you can pass baseurl as "http://username:password#localhost:4040".
Hope it works
In C# Selenium Web Driver I have managed to get it working with the following code:
var alert = TestDriver.SwitchTo().Alert();
alert.SendKeys(CurrentTestingConfiguration.Configuration.BasicAuthUser + Keys.Tab + CurrentTestingConfiguration.Configuration.BasicAuthPassword);
alert.Accept();
Although it seems similar, the following did not work with Firefox (Keys.Tab resets all the form and the password will be written within the user field):
alert.SendKeys(CurrentTestingConfiguration.Configuration.BasicAuthUser);
alert.SendKeys(Keys.Tab);
alert.SendKeys(CurrentTestingConfiguration.Configuration.BasicAuthPassword);
Also, I have tried the following solution which resulted in exception:
var alert = TestDriver.SwitchTo().Alert();
alert.SetAuthenticationCredentials(
CurrentTestingConfiguration.Configuration.BasicAuthUser,
CurrentTestingConfiguration.Configuration.BasicAuthPassword);
System.NotImplementedException: 'POST
/session/38146c7c-cd1a-42d8-9aa7-1ac6837e64f6/alert/credentials did
not match a known command'
Types of popups are defined in webdriver alerts: https://www.selenium.dev/documentation/webdriver/browser/alerts/
Here it is another type - authentication popup - eg generated by Weblogic and not seen by Selenium.
Being HTTPS the user/pass can't be put directly in the URL.
The solution is to create a browser extension: packed or unpacked.
Here is the code for unpacked and the packing procedure: https://qatestautomation.com/2019/11/11/handle-authentication-popup-in-chrome-with-selenium-webdriver-using-java/
In manifest.json instead of “https://ReplaceYourCompanyUrl“ put “<all_urls>”.
Unpacked can be used directly in Selenium:
#python:
co=webdriver.ChromeOptions()
co.add_argument("load-extension=ExtensionFolder")
<all_urls> is a match pattern
The flow for requests is in https://developer.chrome.com/docs/extensions/reference/webRequest/
We can also update browser setting to consider logged in user -
Internet Options-> Security -> Security Settings-> Select Automatic login with current user name and password.
The following Selenium-Webdriver Java code should work well to handle the alert/pop up up window:
driver.switchTo().alert();
//Selenium-WebDriver Java Code for entering Username & Password as below:
driver.findElement(By.id("userID")).sendKeys("userName");
driver.findElement(By.id("password")).sendKeys("myPassword");
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
I used IE, then create code like that and works after modification several code:
public class TestIEBrowser {
public static void main(String[] args) throws Exception {
//Set path of IEDriverServer.exe.
// Note : IEDriverServer.exe should be In D: drive.
System.setProperty("webdriver.ie.driver", "path /IEDriverServer.exe");
// Initialize InternetExplorerDriver Instance.
WebDriver driver = new InternetExplorerDriver();
// Load sample calc test URL.
driver.get("http://... /");
//Code to handle Basic Browser Authentication in Selenium.
Alert aa = driver.switchTo().alert();
Robot a = new Robot();
aa.sendKeys("host"+"\\"+"user");
a.keyPress(KeyEvent.VK_TAB);
a.keyRelease(KeyEvent.VK_TAB);
a.keyRelease(KeyEvent.VK_ADD);
setClipboardData("password");
a.keyPress(KeyEvent.VK_CONTROL);
a.keyPress(KeyEvent.VK_V);
a.keyRelease(KeyEvent.VK_V);
a.keyRelease(KeyEvent.VK_CONTROL);
//Thread.sleep(5000);
aa.accept();
}
private static void setClipboardData(String string) {
// TODO Auto-generated method stub
StringSelection stringSelection = new StringSelection(string); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
}

Selenium WebDriver page object

Quick question about page objects in selenium webdriver. our site is very dynamic with lots of ajax and various authentication states. It is tough to figure out how to define each page object BUT lets say I have figured that out and defined several page objects that represent our site.
How do you handle crossing from page to page. So I get a page object for my home page and one for my account page and one for my results page. Then I need to write a test that traverses all my pages to simulate a user performing multiple actions.
How do you say give me a HomePage object to create a new use -> then get a account page object to go perform some user actions - then get a result page object to verify those actions all from a single script.
How are people doing this?
thanks
When you're simulating having the user enter a new URL into the URL bar of the browser, then it's the responsibility of the test class to create the page object it needs.
On the other hand, when you're doing some operation on the page that would cause the browser to point to another page -- for example, clicking a link or submitting a form -- then it's the responsibility of that page object to return the next page object.
Since I don't know enough about the relationships between your home page, account page, and result page to tell you exactly how it'd play out in your site, I'll use an online store app as an example instead.
Let's say you've got a SearchPage. When you submit the form on the SearchPage, it returns a ResultsPage. And when you click on a result, you get a ProductPage. So the classes would look something like this (abbreviated to just the relevant methods):
public class SearchPage {
public void open() {
return driver.get(url);
}
public ResultsPage search(String term) {
// Code to enter the term into the search box goes here
// Code to click the submit button goes here
return new ResultsPage();
}
}
public class ResultsPage {
public ProductPage openResult(int resultNumber) {
// Code to locate the relevant result link and click on it
return new ProductPage();
}
}
The test method to execute this story would look something like this:
#Test
public void testSearch() {
// Here we want to simulate the user going to the search page
// as if opening a browser and entering the URL in the address bar.
// So we instantiate it here in the test code.
SearchPage searchPage = new SearchPage();
searchPage.open(); // calls driver.get() on the correct URL
// Now search for "video games"
ResultsPage videoGameResultsPage = searchPage.search("video games");
// Now open the first result
ProductPage firstProductPage = videoGameResultsPage.openResult(0);
// Some assertion would probably go here
}
So as you can see, there's this "chaining" of Page Objects where each one returns the next one.
The result is that you end up with lots of different page objects instantiating other page objects. So if you've got a site of any considerable size, you could consider using a dependency injection framework for creating those page objects.
Well, I created my own Java classes which represent the pages:
Say, the below is code to represent home page. Here user can login:
public class HomePage{
private WebDriver driver;
private WebElement loginInput;
private WebElement passwordInput;
private WebElement loginSubmit;
public WebDriver getDriver(){
return driver;
}
public HomePage(){
driver = new FirefoxDriver();
}
public CustomerPage login(String username, String password){
driver.get("http://the-test-page.com");
loginInput = driver.findElement(By.id("username"));
loginInput.sendKeys(username);
passwordInput = driver.findElement(By.id("password"));
passwordInput.sendKeys(password);
loginSubmit = driver.findElement(By.id("login"));
loginSubmit.click();
return new CustomerPage(this);
}
}
And the page for Customer can look like this. Here I am demonstrating, how to get, say, logged in user:
public class CustomerPage{
private HomePage homePage;
private WebElement loggedInUserSpan;
public CustomerPage(HomePage hp){
this.homePage = hp;
}
public String getLoggedInUser(){
loggedInUserSpan = homePage.getDriver().findElement(By.id("usrLongName"));
return loggedInUserSpan.getText();
}
}
And the test can go like this:
#Test
public void testLogin(){
HomePage home = new HomePage();
CustomerPage customer = home.login("janipav", "extrasecretpassword");
Assert.assertEquals(customer.getLoggedInUser(), "Pavel Janicek");
}
You generally want to model what a user actually does when using your site. This ends up taking the form of a Domain Specific Language (DSL) when using page objects. It gets confusing with reusable page components though.
Now that Java 8 is out with default methods, reusable page components can be treated as mixins using default methods. I have a blog post with some code samples found here that explains this in more detail: http://blog.jsdevel.me/2015/04/pageobjects-done-right-in-java-8.html
I suggest you use a framework that provides support for these patterns. Geb is one of the best one out there. Below is an example taken from their manual
Browser.drive {
to LoginPage
assert at(LoginPage)
loginForm.with {
username = "admin"
password = "password"
}
loginButton.click()
assert at(AdminPage)
}
class LoginPage extends Page {
static url = "http://myapp.com/login"
static at = { heading.text() == "Please Login" }
static content = {
heading { $("h1") }
loginForm { $("form.login") }
loginButton(to: AdminPage) { loginForm.login() }
}
}
class AdminPage extends Page {
static at = { heading.text() == "Admin Section" }
static content = {
heading { $("h1") }
}
}
I enjoy writing Selenium Webdriver tests using the Page Object pattern. But was personally annoyed at the verbosity and repetition of having to always explicitly instantiate and return the next page or page component. So with the benefit of Python's metaclasses I wrote a library, called Keteparaha, that automatically figures out what should be returned from a selenium page object's method calls.

Why is Google App Engine servlet getting old no more exist data from file?

I have a Google App Engine servlet, it's supposed to go to my site and get a file, then display the content of that file on the servlet served page, the code looks like this :
public class My_Servlet extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super.init(config);
System.gc();
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/html");
out.println(getTextFile());
}
String getTextFile()
{
String Text="";
try
{
URL url=new URL("http://example.com/A_Dir/Test.txt");
BufferedReader reader=new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line=reader.readLine())!=null) { Text+=line+"<Br>"; }
reader.close();
}
catch (Exception e) { }
return Text;
}
It worked, but the problem is, after I changed the content in the file "Test.txt" on my site, the Google App is still displaying old data, I checked and double checked the file on my site, the old data is no longer there, and I thought every time I clicked the link served by the GAE, it will call getTextFile(), create the URL, go get the file and parse the lines, but it seems GAE is remembering old data from 3 days ago, and no matter how many times I refreshed the page or updated the GAE app and reloaded it on to the App Engine [and I can see the change made to the updated servlet], it's still serving the more then 3 day old data, why? How to force it dynamically load that file?
GAE is caching the file. Try:
URL url=new URL("http://mysite.com/A_Dir/Test.txt?r="+System.currentTimeMillis());

GWT form upload using BlobstoreService App Engine

I am using GWT and Google App Engine Java for my application. I have a profile screen where
user enters profile information like name, age and address, saves it and gets success or failure message. I developed this initial application using GWT-RPC and it worked fine. I had a new requirement where I have to store image of the user. I am using BlobstoreService to store images. This has created complications in the flow. I had to use FormPanel as it is the only way to do a FileUpload in GWT. The BlobStore service servlet expects a redirect on completion. As a result it cannot now return any status back to my GWT application once the profile is saved. Is there easy to store images using GWT along with other form fields and show a status message back to user once the profile is saved.
i struggled a lot with this problem until yesterday I figured out the solution with much help from Ikai Lan's blog. Basicaly what I did is follow his steps but with a few modifications because doing it exactly how he did it did'nt work for me:
Create a form panel : set encoding multipart, method post.
Make a GWT Remote Service that just has one method:public String getUploadURL() or something like that and in the IMPL write this:
BlobstoreService service = BlobstoreServiceFactory.getBlobstoreService();
return service.createUploadUrl("/XXX/YYY");
In XXX you must put your project path, for example mine is com.fer.pyn.PictureYourNews
In YYY you must put the servlet mapping name for a new servlet that we will have to create: I put XXX = BlobUploader, I created a BlobUploader extends HttpServlet and you have to update the web.xml.
Okey, so this is the weird part that I could'nt figure out, thing is that when we make a RPC call to getUploadURL() in the remote ervice from step 2 that returns a weird addres, like: '/_ah/img/eq871HJL_bYxhWQbTeYYoA' and that is the .fromAction you have to put in your form from step one. You need to update the form's action every time so i suggest the following:
public void initBlobStoreSession()
{
imageService.getBlobStoreUploadURL(new AsyncCallback()
{
#Override
public void onSuccess(String result) {
uploadFormPanel.setAction(result);
System.out.println("Upload Form Panel Action set");
}
#Override
public void onFailure(Throwable caught) {
//oops
}
});
}
So when you submit your fromPanel, IT WILL UPLOAD THE BLOB and you dont have to do anything, the tricky part is how to get the blob:
What you need to do now is create the YYY servlet we where talking about in step 4.
In the post method, this is important:
private BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, BlobKey> blobMap = blobService.getUploadedBlobs(request);
BlobKey blobKey = blobMap.get(UPLOAD_WIDJET_NAME);
UPLOAD_WIDJET_NAME is the .setName for the FileUpload widjet.
What you are doing there is getting a key for yout BLob so you can reference it later.
Our next step is showing the uploaded image back to the GWT layer:
//In the same post method from step 7
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String imageURL = imagesService.getServingUrl(blobKey);
response.sendRedirect("/XXX/YYY?imgURL="+imageURL);
Now in the get method:
String imageUrl = request.getParameter("imgURL");
response.setHeader("Content-Type", "text/html");
response.getWriter().println(imageUrl);
We are done, now you just have to
uploadFormPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
#Override
public void onSubmitComplete(SubmitCompleteEvent event) {
uploadFormPanel.reset();
initBlobStoreSession();
String imageUrl = event.getResults();
Image image = new Image();
image.setUrl(imageUrl);
//if you are using jetty, leave this on
//or else it wont work
//Don't use GWT.getModuleBaseURL(), it doesnt
//work well in development mode
imageUrl.replace("http://0.0.0.0:8888/", "");
System.out.println(imageUrl);
final PopupPanel imagePopup = new PopupPanel(true);
imagePopup.setWidget(image);
// Add some effects
imagePopup.setAnimationEnabled(true); // animate opening the image
imagePopup.setGlassEnabled(true); // darken everything under the image
imagePopup.setAutoHideEnabled(true); // close image when the user clicks
imagePopup.center(); // center the image
}
});
check out upload4gwt which address uploading in GWT on AppEngine.
(disclosure: I created upload4gwt; it's not mature yet, however may be useful)
I had the same problem. As a workaround I'm using a redirection to a servlet that print a status message for the client to parse.
I'm passing the websafe string representation of the key to that result servlet.
That's a bit hackey, I'd like someone to come with a better answer, or explain why the blobstore servlet have to redirect.
Yeah, things get more complicated with uploads in GWT.
You can save the form data and image in separate RPCs, and either include a status message in the response to the image upload, or fire off a 3rd RPC when the form returns to get any status or metadata you need.

Resources