Is any way to get captcha value and store it in a variable? - selenium

I am Using Selenium RC in C# My application has 3 Captha image in Different registration pages. i have a Huge script i will run it on night hours(when i am off). Hence i feel it will be better if i can capture the last displaying captcha image value and store it in to a variable so that i can input on the relevant field. Is it Possible ?

A Captcha cannot be automated that easily. That is why they were invented!!
Alas... You can use code hackers and Image Recognition patterns and scripts. It is called OCR. http://en.wikipedia.org/wiki/Optical_character_recognition
I will not write more about this as i wont encourage others for hacking ideas. But... First: Google is your friend. Second: In my testing environment we used a captcha with a static value. And then before it went live we removed set it again for the algorithm.
Gergely.

Have a hidden field (with a non-obvious name) that contains a strongly encrypted copy of the text used in the captcha then on your selenium test system decode it using the private key?

For testing purposes you will need to send the captcha value in a machine readable manner, by sending it as an additional field. You would then need to remove the field after the tests are done.

Make use of the 'input' tag with type 'hidden' in-order to handle Captcha.
JavascriptExecutor js = (JavascriptExecutor) driver;
//Set the captcha values using setAttribute
js.executeScript("document.getElementsByName('xxxx')[0].setAttribute('value', 'xxxx')");
driver.findElement(By.name("xxxx")).sendKeys("xxxx");

Related

ID's Required for UI automation in selenium in all html pages

There is a project where we are going to automate the UI but the Automation team is suggesting that we have to use ID's all over the page so that it will be easy to automate there script.
My Question here is why we will use ID's everywhere ? hampering the Html and Css structure.
The webpage can be automated without ID's in html yes or no ?
Yes, a web page can be automated without ID's. For example, you can play with cssSelectors here https://www.w3schools.com/cssref/trysel.asp (note that example page has elements with and without ids)
Using ids for element's lookup in automation is generally considered as a best practice. If you use ids your automation tests will become independent of html structures which basically will make them more stable.
For example, in the first version of your app you may have some text implemented as
<p id="someTextId" class="someClass">Hello world</p>
but at some point may decide to rewrite it as (change the tag and even apply different class name)
<div id="someTextId" class="anotherClass">Hello world</div>
In case you rely on id #someTextId to locate an element your test will still be able to access necessary element and interact with it properly. If you use p or .someClass your automation test will fail to find an element even though from the ui perspective the same text will be displayed in a browser.
We faced several downsides of using id:
Some frameworks do not recommend using them or generate them automatically. (Potential issues with ids described here https://www.javascriptstuff.com/use-refs-not-ids/, https://www.quora.com/Are-IDs-a-big-no-no-in-the-CSS-world, https://dev.to/claireparker/reasons-not-to-use-ids-in-css-4ni4, https://www.creativebloq.com/css3/avoid-css-mistakes-10135080, https://www.reddit.com/r/webdev/comments/3ge2ma/why_some_people_dont_use_ids_at_all/)
Some other logic may rely on them, so changing/adding them for the need of automation may somehow affect other app logic unexpectedly.
What you can use instead of id is some other attribute. For example in our projects, we have switched from id to a specific attribute named dataSeleniumId. It clearly shows that the attribute is selenium tests usage only. As a next step, you can add a rule in your team when someone changes or removed dataSeleniumId attribute he should inform automation testing team about it. As changing/removing this attribute will lead to test failures and to avoid any false failures it is better to fix it in advance.
For an automation developer its much easier to browse trough the html code and see the id of specific button/text field/etc.. to implement the relevant locator inside the automated test.
In most cases, the project start to receive duplication of classes or complicated nested elements. This make the life of automation dev harder, because of writing xpath or css selectors, verify that they work and this locator finds only 1 unique element.
Its up to the team and code style suggested from the team leader.
Back on the question, yes the website can be written without id's but if the goals is to automate large part of the website, id's would be great helper to the automation dev team.

How to check in jmeter if entered fields remain same in the first page after navigating back from nth page

I want to test a page.Where i want to fill up the fields like first name last name etc.and after going two pages further if i come back to the original page by using back navigation ,data entered for first name and last name remains the same.or it is filled up.
In jmeter i want to check the same if data entered for the fields remain same if i navigate back .
How can i achieve this.
I tried gving url directly in the path its not happening since it is not the way.
please help me since i'm new to jmeter.
You need to understand 2 things. How JMeter works and how your application works.
JMeter only captures data that is communicated to server. It does not matter how data is entered from UI. It does not check if data retains in the fields or not. It only records the request that is sent by your application to server-side.
So, if you understand above, you also need to understand how your application sends data to server. Does it sends the request as you move from first page to second. Or does it send (Submit) data on final page.
Either way, JMeter is not a tool to test if your form fields are retaining data in them as you navigate between pages. As mentioned earlier it only monitors data requests/responses.
Selenium seems a better option for your test requirement.
Please read the apache documentation carefully:
JMeter is not a browser. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it's possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever viewed at a time).
First, you have to understand how JMeter works!!! To do the Functional Testing, Selenium would be a good choice.
Thanks

how to get the data from captcha in selenium webdriver

I'm using Selenium webdriver (Java).
I need to test the registration form but before submitting, image box (captcha) is appearing but everytime of execution it is going to be changed. I want to know how to get the data from image (captcha).
Anyone can help me?
If the captcha is coming from an environment under your control, you will likely need to implement some sort of method indicating you are in a test environment and have the captcha system return a known value or some indicator of what the expected value is.
If, on the other hand, the captcha is coming from another source out of your control, you are probably our of luck. At that point, you are essentially in the same boat as the spammers who are in a constant arms race to write software that can visually parse a captcha.
UPDATE
I feel the need to add some clarification to the ideas put forth in the question, answer and comments. Essentially you are dealing with one of the following situations (note that when I say 'your', I am referring to you, your company, client, etc):
1) Your form, Your captcha system: If this is the case, your best solution is to work with your developers to add a 'test' mode to your captchas, returning either a known value, or additional information in the page that indicates what the expected value should be. If you are able to make use of a tool, either written by you, or by another, that can successfully 'read' the captcha image, your system is broken. If you can do it in test mode, what is to stop anyone else (spammer, hacker, etc) from bypassing your captcha in exactly the same manner.
2) Your form, 3rd Party captcha system: If this is the case, your best solution is again to see if the system has some 'test' mode that you can make use of. I have no experiance with these systems myself but in general would guess that test methods exist for the major systems out there. A Google search of {Captcha System Name} automated testing should return some good hints as to how to go about testing with the system. If nothing good comes from that, your next bet would be to implement your own, internal, test only, dummy captcha system that works with some known value and make your captcha provider configurable so that you can point to your test system in test/dev/etc and your real system in production.
3) Another Form, Unknown captcha system: I am going to make a leap of faith here and assume this is not your case, but just for completeness I will include it. If this is your case, your not testing anything at all and are simply asking for help bypassing someone else's security mechanisms for your own reasons. If that is the case, please seek your assistance on less scrupulous sites.
Captcha code was introduced in order to prevent from the robot or automation codes. There is no option for automating the Captcha code.
1 . You can give a wait time for the automation, so that the user can enter the captcha code.
2. If the project is in testing url means, you can request your system admin and developer to disable the captcha validation.
May be this can help you, but i din't try on this..
Developers will generate a random value for captcha, and they will convert the value into image as well as they will store the value in session for comparing the entered input is matching with the captcha code.
So If possible, you can take that session value and give as the input.

Need your ideas to automate

In my project we have a web based tool where it collects all the exceptions that occur in log. The list of exceptions will be displayed in table and a icon will be displayed in a column to get the stack trace. Some one needs to check all those exceptions and we need to assign to different teams based on the keywords.
Eg : If the stacktrace contains "DB Connection error" it has to be assigned to DB team, if it contains code error like "classcast exception" in particular file then it has to be assigned to individual owning the module. Currently some one has to open each n every row in the table displayed and open the stack trace and do Ctrl + F and if any of those keywords are found assign it to respective team.
We don't have any access to the tool's database and it's web based. I need some good ideas to automate it. Please suggest me with which technology and idea to automate it.
Sounds like selenium + programming language of your choice is a good fit (even without a selenium).
You need to:
download source code of the page containing exceptions table.
Use any of these: wget, curl, selenium, urllib for python, etc.
put it into variable and extract exceptions list in convenient to you format.
Use any xpath-powered tool, like selenium, lxml for python, etc.
do your thing with the extracted exceptions list: search for keywords, assign tickets, etc.
Use programming language of your choice.
Definitely you can achieve this using selenium.
Following are the steps to follow.
Supply date criteria with type command to the boxes text boxes element.
click search button.
store the required text in variable using storeValue(locator, variableName).
click on image link if you are able to keep track of popup window else you have to use
$second=$sel->getAttribute("//html/body/.../a#href"); and now your $second has link address $sel->openWindow($second,"MyWindow2"); now select that new window $sel->selectWindow("MyWindow2"); and do operation of storing required data from the popup.
Now agian for selection of previous window use `$sel->selectWindow("null"); // hear $sel is object of selenium (i.e. $this).
Select your required option using select(selectLocator, optionLocator).
Use selenium RC for above steps so that you have more manipulation with the data stored and as per requirement operations.
In above steps syntax are of PHPUnit.
Please try the steps may help you.
Regards.

How to Verify whether a Robot is Entering Information

I have a web form which the users fill and the info send to server and stored on a database. I am worried that Robots might just fill in the form and I will end up with a database full of useless records. How can I prevent Robots from filling in my forms? I am thinking maybe something like Stackoverflow's robot detection, where if it thinks you are a robot, it asks you to verify that you are not. Is there a server-side API in Perl, Java or PHP?
There are several solutions.
Use a CAPTCHA. SO uses reCAPTCHA as far as I know.
Add an extra field to your form and hide it with CSS (display:none). A normal user would not see this field and therefore will not fill it. You check at the submission if this field is empty. If not, then you are dealing with a robot that has carefully filled out all form fields. This technique is usually referred to as a "honeypot".
Add a JavaScript timer function. At the page load it starts a value at zero and then increases it as time passes. A normal user would read and fill out your form for some time and only then submit it. A robot would just fill out and submit the form immediately upon receiving it. You check if the value has gone much from zero at the submission. If it has, then it is likely a real user. If you see just a couple of seconds (or even no value at all due to the robots not executing JavaScript) then it is likely a robot. This will however only work if you decide you will require your users have JavaScript on in order to perform "write" operations.
There are other techniques for sure. But these are quite simple and effective.
You can use reCAPTCHA (same as stackoverflow) - they have libraries for a number of programming languages.
I've always preferred Honeypot captcha (article by phil haack), as its less invasive to the user.
Captchas bring accessibility problems and will be ultimately defeated by software recognition.
I recommand the reading of this short article about bot traps, which include hidden fields, as Matthew Vines and New in town already suggested.
Anyway, you are still free to use both captcha and bot traps.
CAPTCHA is great. The other thing you can do that will prevent 99% of your robot traffic yet not annoy your users is to validate fields.
My site, I check for text in fields like zip code and phone number. That has removed all of the non-targeted robot misinformation.
You could create a two-step system in which a user fills the form, but then must reply to an e-mail to "activate" the record within a set period of time - say 24 hours.
In the back end, instead of populating your current table with all the form submissions, you could put them into a temporary table that automatically deletes any row that is older than your time allotment. Unless you have a serious bot problem, then I would think that the table wouldn't get that big, especially if the first form is just a few fields.
A benifit of this approach is that you don't have to use captcha or some other technology like that that might create some accessibility problems.