Is it possible to add selector text to a By object - selenium

I am wondering if it is possible to add selector text to an existing By object.
That is, suppose I have a popup with a class name ".popup". But then I want to be a little more specific as the webapp is utilized. Now, our popup has an added class name ".cart". My new selector wants to be ".popup.cart". Here is what I have. Is there a better or more efficient way to do this? (I'm coding in C#, but it can be relevant to any language.)
By selector= By.CssSelector("div.popup");
selector = By.CssSelector(
selector.ToString().Remove(0, selector.ToString().LastIndexOf(":") + 1)
+ ".cart");
What is going on above, selector becomes "div.popup".
selector.ToString() returns "By.CssSelector: div.popup"
So I remove all text from the beginning to the last ":" and add the remaining text to ".cart".
selector then becomes "div.popup.cart", which is what I want.
The problem comes when I try to combine any selectors that have been "ByChained." The ToString() method will return By.Chained([By.CssSelector: .popup, By.CssSelector: .title])
If it helps, I am also Sizzle. So, if there's a JQuery way to do this, I'm open.

Put the "div.popup" in a string say "str" like this:-
String str = div.popup
Then you can append ".cart" to the string, make it "div.popup.cart", and use the cssSelector as:
By selector= By.cssSelector(str+".cart");
Note:- Above is a java code.

As mentioned above, I'm basically solving this by unrolling the By.CssSelector().ToString() output and/or the ByChained().ToString() output. There are some caveats. Mainly that the last selector in a ByChained() must be of type By.CssSelector (since I'm adding a JQuery pseudo class):
private static By AddPseudoFunction(By selector, string pseudoFunc)
{
string type = selector.GetType().ToString();
string selectorValue = selector.ToString();
if (type.Contains("Chained"))
{
selectorValue = selectorValue.Remove(0, selector.ToString().IndexOf("[") + 1);
int start = selectorValue.LastIndexOf("]");
int delta = selectorValue.Length - start;
selectorValue = selectorValue.Remove(start, delta);
}
StringBuilder builder = new StringBuilder();
foreach (string s in selectorValue.Split(','))
{
builder.Append(" ");
builder.Append(s.ToString().Remove(0, s.ToString().LastIndexOf(":") + 2));
}
selectorValue = builder.ToString().TrimStart(' ') + pseudoFunc;
return new ByJQuery.ByJQuerySelector(selectorValue, true);
}
Of course, if you want to make this so that it will return a By.CssSelector, just replace the return with:
return By.CssSelector(selctorValue + addSelector);
Where addSelector would be a valid CSS Selector value (like what I describe in the question).

Related

Should we have one action class in selenium keyword driven framework even though we have a lot of methods?

As per my understanding, keyword driven framework is, we create a keyword for each action we do and write the test cases in excel using those keywords.
For example, opening browser, entering username, password, clicking on login button etc we create a keyword for each action and create a method for each of these keywords and store all these methods in a class like actionmethods() etc.
We use java reflection class to call these methods.
If we have less no. of methods that should be ok. I am working on a small project where I got like 200 keywords. So I have to write 200 methods here. Should I store all these methods in one class?
What if I have 1000 keywords (for a big project)?
If I create separate files grouping keyword methods based on the pages, it is becoming very complicated. Can someone please explain if we use only one class to hold all the methods?
Thank you.
Maintain the keyword methods as separate class for each page like we do in page object pattern.
While calling the keyword, we can specify the class name as well along with method name. For e.g., LoginPage.login
For e.g., if you are maintaining the page class under package com.myproject.test.pages You can change the reflection code for invoking as,
public Object invokeKeywordMethod(String keywordName)
throws InvocationTargetException, IllegalAccessException, InstantiationException {
String[] keywords = keywordName.split("\\.");
if (keywords.length == 1)
throw new Error("Invalid keyword: " + keywordName + ". The keyword must be as ClassName.methodName");
String className = keywords[0];
String methodName = keywords[1];
Class<?> pageClass = getPageClass(className);
Method method;
try {
method = getPageClass("").getDeclaredMethod(methodName);
} catch (NoSuchMethodException e) {
throw new Error("The keyword method '" + methodName + "' is not found in the class");
}
return method.invoke(pageClass.newInstance());
}
private Class<?> getPageClass(String className) {
Class<?> pageClass = null;
try {
pageClass = Class.forName("com.myproject.test.pages." + className);
} catch (ClassNotFoundException e) {
throw new Error(className + " not found in package 'com.myproject.test.pages' ");
}
return pageClass;
}

Plugin: How to insert new method in existing PHP class?

I'm trying to create a IntelliJ plugin (mostly for learning purposes). My aim is that by pressing a keyboard shortcut the plugin will generate a corresponding PHP unit test method stub in the test file.
So let's say Db.php is open, the upon pressing Ctrl+Shift+U the plugin will create a unit test stub in DbTest.php.
So far I've figured out how to get the method name at cursor and how to locate the corresponding Unit test file (i.e. Db => DbTest) as PsiFile.
PsiFile[] search = FilenameIndex.getFilesByName(project, testFileName, scope); //scope is the test directory
PsiFile testFile = search[0];
What I cannot figure out is how to insert the generated new method stub this in testFile and then save the changes?
P.S. I see there exists a createMethodFromText function but how do I get the PsiClass from PsiFile? Also how do I save the changes?
There're just a few simple steps.
Find PhpClass you want to insert a new method in. As you already have PsiFile you can either traverse a tree manually or use PhpElementVisitor.
1.1. To travers a tree manually you can use PsiTreeUtil#findChildOfType method. In your case you'll need to find GroupStatement first, then the class you need.
1.2. Invoke PsiElement#accept method (PsiFile is an instance of PsiElement) provided with PhpElementVisitor with overridden #visitPhpGroupStatement and #visitPhpClass methods.
Use PhpPsiElementFactory#createMethod to create the new method from text. Note that this class isn't a part of the public API, so theoretically it can be easily changed/moved/removed/whatever in the future.
Use PsiElement#add (PhpClass is also an instance of PsiElement) to insert the method into the class.
That's all. You don't need to explicitly save the changes.
Here is what worked for me in the end. Thanks everyone for the help
for (int i = 0; i < found.getTextLength(); i++) {
PsiElement ele = found.findElementAt(i);
PhpClass phpClass = PsiTreeUtil.getParentOfType(ele, PhpClass.class);
if (phpClass != null) {
Method methodExists = findMethod(phpClass, methodName);
if (methodExists == null) {
new WriteCommandAction.Simple(phpClass.getProject(), phpClass.getContainingFile()) {
#Override
protected void run() throws Throwable {
PsiElement brace = phpClass.getLastChild();
if (brace != null) {
Method method = PhpPsiElementFactory.createMethod(phpClass.getProject(), "public function " + methodName + "() {\n\n}");
CodeStyleManager styleManager = CodeStyleManager.getInstance(getProject());
styleManager.reformat(method);
PsiElement newMethod = phpClass.addBefore(method, brace);
PsiNavigateUtil.navigate(newMethod);
}
}
}.execute();
} else {
PsiNavigateUtil.navigate(methodExists);
}
break;
}
}

New ArrayList filtering from another ArrayList using a String as Filter

In my program, a Die (dice for embroidery) is a class with different fields. One of them is of the type String and it is called haveIt. So, if the user of the program enters the word "Yes" on the haveIt field, he should be able to track a list of all the Dies he has, on the myInventory list.
How do I do this? Should I create the myInventory ArrayList<Die> on the fields and constructor of my Contoller class or should I built it inside a special method in that class?
I have tryed everything and nothing works. But I am really new on this.
Here is my last attempt, creating a loop to create the new ArrayList<Die> (that has "Yes" on the haveIt field) from a special getMyInventory method in my Controller class:
public ArrayList<Die> getMyInventory(Die anyDie) {
for (int counting = 0; counting <
diesBigList.Count ; counting++);
{
if
(((Die)diesBigList[counting]).doIHaveIt.contains("Yes"))
myInventory.add(diesBigList[counting]);
return myInventory;
}
}
It does not compile. It tells me that the result should be an Array type but it is resolved as ArrayList... (and I do not comprendo that).
Thanks in advance.
Your missing a return statement. What if this is never true?
if (((Die)diesBigList[counting]).doIHaveIt.contains("Yes"))
you never reach your return statement
Here is the answer
public ArrayList<Die> getMyInventory(Die anyDie) {
ArrayList<Die> myInventory = new ArrayList<Die>();
for (int counting = 0; counting < diesBigList.Count; counting++) {
if (((Die)diesBigList[counting]).doIHaveIt.contains("Yes")) {
myInventory.add(diesBigList[counting]);
}
}
return myInventory;
}
Also there could be a problem with this: diesBigList.Count I have no idea where you got that object or what it's methods looks like but I presume in my code your making that call correctly.

My Nintex Custom Action not reading in Workflow Variable in SingleLineInput

I've written a custom workflow action that takes in several values, mostly using the SingleLineInput control.
When I assign literal values, I have no issues, but when I try to assign a Workflow Variable, I don't get the actual value of the variable, I get the literal text - something like {WorkflowVariable:XmlValue} - assuming my variable was names XmlValue.
I'm not sure what I could possibly be doing wrong. Any ideas?
Here's code snippets:
The javascript for retrieving the value from the SingleLineInput
function TPAWriteConfig() {
configXml.selectSingleNode("/NWActionConfig/Parameters/Parameter[#Name='FieldValue']/PrimitiveValue/#Value").text = getRTEValue('<%=fieldValue.ClientID%>');
SaveErrorHandlingSection();
return true;
}
The server control:
<Nintex:ConfigurationProperty ID="ConfigurationProperty3" runat="server" FieldTitle="Field Value" RequiredField="True">
<TemplateControlArea>
<Nintex:SingleLineInput runat="server" id="fieldValue"></Nintex:SingleLineInput>
</TemplateControlArea>
</Nintex:ConfigurationProperty>
From my adapter class:
private const string FieldValueProperty = "FieldValue";
NWActionConfig config = new NWActionConfig(this);
config.Parameters[2] = new ActivityParameter();
config.Parameters[2].Name = FieldValueProperty;
config.Parameters[2].PrimitiveValue = new PrimitiveValue();
config.Parameters[2].PrimitiveValue.Value = string.Empty;
config.Parameters[2].PrimitiveValue.ValueType = SPFieldType.Text.ToString();
From the activity class:
public static DependencyProperty FieldValueProperty = DependencyProperty.Register("FieldValue", typeof (string),
typeof (
WriteOnePdfFieldActivity));
public string FieldValue
{
get { return (string) GetValue(FieldValueProperty); }
set { SetValue(FieldValueProperty, value); }
}
I feel a little silly answering my own question, but for the sake of anyone else having the same issues. Here's how it works:
If you're putting a literal value in the field, just use the value
If you're using any other kind of assignment, do a lookup based on the value.
The code below demonstrates:
var fieldValue = FieldValue.StartsWith("{") ? ctx.AddContextDataToString(FieldValue, true) : FieldValue;
This extract the value from the workflow context. Hope this helps.

Is it possible to pass a variable's name along with the value, when passing through functions?

I want to know if it's possible to retrieve the variables name from when it was passed into a certain function. For example, if I call parseId(myId) to a function with the signature parseId(id), i can obviously retrieve the value of 'id'. However, is there any way I can retrieve 'myId' as a string (without passing it as another value)?
Specifically in vb.net, but I'm interested in how it would work in any given language.
This is all just random thoughts.. feel free to dismiss or not ;-p
Re your comment about use with stored procedures... if you want to go that route, I wouldn't mess around with the local variable names; that is an implementation detail. However, you could expose those details on an interface method and use the names from there, since that is more formalised - for example (C#):
interface ICustomerRepository {
Customer GetById(int id); // perhaps an attribute to name the sproc
}
You can use similar expression-tree parsing (as discussed here) to get the name and value of the parameter, for example:
var repoWrapper = new Repo<ICustomerRepository>();
int custId = 12345;
var cust = repoWrapper.Execute(r => r.GetById(custId));
Here we'd want to resolve the argument to GetById as "id" (not "custId"), with value 12345. This is actually exactly what my protobuf-net RPC code does ;-p (just don't ask me to translate it to VB - it is hard enough to write it in a language you know well...)
No, you can't do that in the normal sense. What are you really trying to accomplish with this?
You can do this in .NET 3.5 and above using expression trees; I'll knock up a C# example, and try to run it through reflector for VB...
C#:
static void Main()
{
int i = 17;
WriteLine(() => i);
}
static void WriteLine<T>(Expression<Func<T>> expression)
{
string name;
switch (expression.Body.NodeType)
{
case ExpressionType.MemberAccess:
name = ((MemberExpression)expression.Body).Member.Name;
break;
default:
throw new NotSupportedException("Give me a chance!");
}
T val = expression.Compile()();
Console.WriteLine(name + "=" + val);
}
The VB is below, but note that the VB compiler seems to use different names (like $VB$Local_i, not i):
Sub Main()
Dim i As Integer = 17
WriteLine(Function() i)
End Sub
Private Sub WriteLine(Of T)(ByVal expression As Expression(Of Func(Of T)))
If (expression.Body.NodeType <> ExpressionType.MemberAccess) Then
Throw New NotSupportedException("Give me a chance!")
End If
Console.WriteLine((DirectCast(expression.Body, MemberExpression).Member.Name
& "=" & Convert.ToString(expression.Compile.Invoke)))
End Sub