How to get the text from webpage using coded ui - selenium-webdriver

I am new for coded ui and I have got stuck for processing message display on webpage. As we used to get text from web page in selenium using method getText(), is such kind of possibility are available in coded ui.?
I would appreciate your help!!!!
Thanks,
Dani
updated - 5/14/2014
script code:
public void FPInternetExplorer()
{
#region Variable Declarations
HtmlEdit uIUserNameEdit = this.UIFundingPilotPortalWiWindow.UIFundingPilotPortalDocument.UIUserNameEdit;
HtmlEdit uIPasswordEdit = this.UIFundingPilotPortalWiWindow.UIFundingPilotPortalDocument.UIPasswordEdit;
HtmlInputButton uILoginButton = this.UIFundingPilotPortalWiWindow.UIFundingPilotPortalDocument.UILoginButton;
HtmlEdit uISecurityQuestionAnswEdit = this.UIFundingPilotPortalWiWindow.UIFundingPilotPortalDocument.UISecurityQuestionAnswEdit;
HtmlInputButton uIValidateButton = this.UIFundingPilotPortalWiWindow.UIFundingPilotPortalDocument.UIValidateButton;
#endregion
this.UIInternetExplorerEnhaWindow6.LaunchUrl(new System.Uri(this.FPInternetExplorerParams.UIInternetExplorerEnhaWindow6Url));
// Type 'test#test.test' in 'User Name' text box
uIUserNameEdit.Text = this.FPInternetExplorerParams.UIUserNameEditText;
// Type '{Tab}' in 'User Name' text box
Keyboard.SendKeys(uIUserNameEdit, this.FPInternetExplorerParams.UIUserNameEditSendKeys, ModifierKeys.None);
// Type '********' in 'Password' text box
uIPasswordEdit.Password = this.FPInternetExplorerParams.UIPasswordEditPassword;
// Click 'Login' button
Mouse.Click(uILoginButton, new Point(62, 19));
//Code to get the text from div tag. This is the code I have added
HtmlDiv testLabel = new HtmlDiv();
testLabel.SearchProperties[HtmlDiv.PropertyNames.Id] = "SecurityQuestion_AnswerText";
string myText = testLabel.InnerText;
Console.Write("myText " + myText);
// Type 'Computer' in 'SecurityQuestion.AnswerText' text box
uISecurityQuestionAnswEdit.Text = this.FPInternetExplorerParams.UISecurityQuestionAnswEditText;
// Type '{Tab}' in 'SecurityQuestion.AnswerText' text box
Keyboard.SendKeys(uISecurityQuestionAnswEdit, this.FPInternetExplorerParams.UISecurityQuestionAnswEditSendKeys, ModifierKeys.None);
// Click 'Validate' button
Mouse.Click(uIValidateButton, new Point(81, 25));
}
// When run this code using coded UI, system gives exception: Message - 'To test Windows Store apps, use the Coded UI Test project template for Windows Store apps under the Windows Store node.' Is something missing in this code? Would appreciate if you could provide link to configure missing component

The property you're looking for is "InnerText". So, for example, on a hyperlink like:
HtmlHyperlink target = new HtmlHyperlink();
target.SearchProperties[<search property>] = <value>;
string myText = target.InnerText;
You can extrapolate this to just read all of the text on the page by selecting the outer container of the page and getting the InnerText of it, then parse through the string for what you want, but I've found it a lot simpler if you work in the most focused object.

Related

How to create checkbox element in htmlunit?

I am trying to use the createElement method explained in the following link:
http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/InputElementFactory.html#createElement-com.gargoylesoftware.htmlunit.SgmlPage-java.lang.String-org.xml.sax.Attributes-
For this I am trying to use the following code:
HtmlPage page = webClient.getPage("http://...");
HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) page.createElement("checkbox");
But the createElement method returns an HtmlUnknownElement object. How can I create the checkbox element?
The following code is working while creating an input text element:
HtmlElement tmpCheckBox = (HtmlElement) pageClientInput.createElement("input");
Following the suggestion given here I have tried this other way:
HtmlElement tmpInput = (HtmlElement) page.createElement("input");
tmpInput.setAttribute("type", "checkbox");
HtmlRadioButtonInput tmpCheckBox = (HtmlRadioButtonInput) tmpInput;
tmpCheckBox.setChecked(true);
But I am getting an exception casting the HtmlElement to HtmlRadioButtonInput:
java.lang.ClassCastException: com.gargoylesoftware.htmlunit.html.HtmlTextInput cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput
I need an HtmlRadioButtonInput in order to use the setChecked method. HtmlElement doesn't have setChecked method available.
Your code wont work because HtmlPage.createElement can't choose the correct Element Factory without attributes. Which you cannot set through this method.
You can access the correct element factory through InputElementFactory and setting the type as checkbox, as below.
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
HtmlPage page = webClient.getPage("http://...");
//Attribute need to decide the correct input factory
AttributesImpl attributes = new org.xml.sax.helpers.AttributesImpl();
attributes.addAttribute(null, null, "type", "text", "checkbox");
// Get the input factory instance directly or via HTMLParser, it's the same object
InputElementFactory elementFactory = com.gargoylesoftware.htmlunit.html.InputElementFactory.instance; // or HTMLParser.getFactory("input")
HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) elementFactory.createElement(page, "input", attributes);
// You need to add to an element on the page
page.getBody().appendChild(checkBox);
//setChecked like other methods return a new Page with the changes
page = (HtmlPage) checkBox.setChecked(false);
Your createElement call produces an HtmlUnknownElement because there is not checkbox html tag. To create a checkbox you have to create an input with type 'checkbox'.
Start here to read more about html and checkboxes.

UI Automation does not find tabPage inside a Tab Control when property Text is provided

I've simplified this problem as much as I can and the code below can be cut&pasted into a 'test.linq' file and loaded into LinqPad. As it stands running this code in LinqPad will show it working - the intention is to find the pane with AutomationId tabPage1 using the UI Automation framework.
Now comment out the working line and bring in the broken line. Now the tab page cannot be found... the only difference is the tab page is declared with a Text property.
I've found a series of blogs which might indicate that the fault lies with an automation provider that the TabControl has but having decompiled the TabControl source I can't see that this is the case but the base Control implements a handler for WM_GETOBJECT and I'm really not sure where that leads.
Any ideas?
<Query Kind="Statements">
<Reference><RuntimeDirectory>\WPF\UIAutomationClient.dll</Reference>
<Reference><RuntimeDirectory>\wpf\UIAutomationProvider.dll</Reference>
<Reference><RuntimeDirectory>\wpf\UIAutomationTypes.dll</Reference>
<Reference><RuntimeDirectory>\Accessibility.dll</Reference>
<Reference><RuntimeDirectory>\wpf\WindowsBase.dll</Reference>
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference>
<Reference><RuntimeDirectory>\System.Security.dll</Reference>
<Reference><RuntimeDirectory>\System.Configuration.dll</Reference>
<Reference><RuntimeDirectory>\System.Deployment.dll</Reference>
<Reference><RuntimeDirectory>\System.Runtime.Serialization.Formatters.Soap.dll</Reference>
<Namespace>System.Windows.Automation</Namespace>
<Namespace>System.Windows.Forms</Namespace>
</Query>
var tabControl = new TabControl();
tabControl.Name = "tabControl";
// Broken
//tabControl.TabPages.Add(new TabPage() { Name = "tabPage1", Text = "First Tab" });
// Working
tabControl.TabPages.Add(new TabPage() { Name = "tabPage1" });
var form = new Form() { Name = "Form1" };
form.Controls.Add(tabControl);
form.Show();
var desktop = AutomationElement.RootElement;
var frmTest = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "Form1"));
var tabPage1 = frmTest.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "tabPage1"));
tabPage1.Dump("tabPage1");

How to check if a panel contains a form?

I have a bunch of panels out of which one does not have a form. While navigating between the panels I need to check if the form.isDirty(). Obviously it works fine as long as I don't hit the panel with no form on. Its a card layout and I am currently using:
Ext.getCmp('content-panel').getForm().isDirty()
I need to check before executing this line if the panel actually has a form. Is it possible to do this in ExtJS 4?
This code is working for requirement,we can access the 'form' property of the panel.If the panel contains the form then this Property return the form object in return and if the panel doesn't contains the form it return the 'undefined' which satisfied your requirement.
var formFlag = Ext.getCmp('content-panel').form;
if(formFlag === undefined){
console.log('form is absent');
}else{
console.log('form is present');//formFlag is the form object in this case
}
The component query should help you to check if there is a form inside the panel and you have to check if the panel is a form
var panel = Ext.getCmp('content-panel');
//Check if this is a form
var isForm = panel.form
//Check if an inner panel is a form
var hasForm = panel.query('form');
if(isForm && hasForm.length > 0){
//Is or has a form
}
Found a workaround. Posting just in case if anyone else might be looking for same thing.
I split my statement in following
var formCmp = Ext.getCmp('content-panel'); and then
called formCmp.getForm
Note: getForm and getForm() return different values.

Removing a fMath image properties dialog in ckeditor

I am a bit stuck with this so it would be great if you could help.
I am using Drupal 7 and Ckeditor 4.3. I am developing a site which has fmath equation editor integrated.
I have disabled the image button, as I don't want end users being able to upload their own images. On the other hand, users can use fMath to insert equations. The way fMath handles equation insertion is by inserting a img tag.
When users double click this image or when they right click over the image, they access the image properties dialog, where they can change source url of the image and few other things. On the url input text, they could change the source of the image so that they could insert their own images on the page, which is something I don't want.
The have been working with two different unsuccessful approaches until now trying to solve this problem:
Removing elements from the dialog, as the URL input text on the dialog (and the alt text as well).
Trying to disable the dialog itself.
I'd like to use a custom plugin to accomplish the desired behavior as I have different CKeditor profiles in Drupal.
I haven't been successful. Do you know how could I handle this undesirable behavior?
Ok, I ended up with something like this in the plugin.js file:
CKEDITOR.plugins.add( 'custom_doubleclick',
{
init: function( editor )
{
// First part, dialogDefinition allow us to remove the
// "Link" and "Advanced" tabs on the dialog
CKEDITOR.on( 'dialogDefinition', function( ev ) {
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
if ( dialogName == 'image' ) {
dialogDefinition.removeContents( 'Link' );
dialogDefinition.removeContents( 'advanced' );
}
});
// Second part, it disables the textUrl and txtAlt input text boxes
editor.on( 'dialogShow', function( ev ) {
var dialog = ev.data;
var dialogName = dialog.getName();
if ( dialogName == 'image' ) {
//var dialog = CKEDITOR.dialog.getCurrent();
// Get a reference to the Link Info tab.
console.log(dialog)
dialog.getContentElement( 'info','txtUrl' ).disable();
dialog.getContentElement( 'info','txtAlt' ).disable();
}
});
}
});
As you can see I didn't disable the dialog itself, but the non useful elements. I hope this can help to someone else.

How to disable a field or make it readonly in Drupal 7

I am trying to disable couple of fields and make them readonly via hook_page_alter(). I was able to do check if user is viewing the page edit section (the form edit)
$page['content']['system_main']['#node_edit_form'] == TRUE)
then when I tried to disable couple of fields, I found that select list can be disabled by this code:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#attributes']['disabled'] = TRUE;
but if I use the following code it doesn't work:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
I also found that I can not use the same code to disable a text area field:
$page['content']['system_main']['field_my_text_area']['und']['#attributes']['disabled'] = TRUE;
The above code doesn't disable the text area, but the same code can disable the select list!
Then I tried hook_form_alter() to do the same thing, and I was able to disable fields and when I checked the rendered array from $page array, I saw that it shows:
$page['content']['system_main']['field_my_field_name_a_select_list']['und']['#disabled'] = TRUE;
but when I set the same code in hook_page_alter(), it didn't work. Looks like something else will override it, I thought that hook_page_alter() is the last place to change markup.
Any idea what is the best way to disable/readonly any kind of field, inside hook_page_alter() in drupal 7?
Thank you
It works for text fields^
$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;
Like it said in the docs
You can use attributes :
$form['#attributes'] = array('disabled' => TRUE);

Resources