public final By xxx all over the place [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
For my Cucumber/Selenium project I'm using a Page Object Model. There is HomePage, SettingsPage, SearchWidget, etc. Classes look like this:
public class SearchWidget extends Page {
public final By buttonDisplayTypeLoc = By.id("button-displayType");
public final By buttonResultsPerPageLoc = By.id("button-resultsPerPage");
// lots more of the same
}
Is there a more elegant way to keep track of locators (the By's), instead of having long lists of them at the beginning of each Page subclass?
I've tried a separate class Element that holds a Map of a String key and a By locator. The Map could be easily added to and retrieved from. The problem with that, is that using Element.getLocator("key"); doesn't get the IDE's help with spelling for the key anymore. So I scratched that.
There's already a Site class that holds things like URL's, etc. I could easily put it there, and although it does pertain to the site, it doesn't sit well with me to have a whole bunch of fields that I'd rather have in the class it pertains to. Although it would be kind of out of sight ...
I looked at using a factory model, but it seems like overkill.
So, again, is there a more elegant way to do this, or should I just leave it alone? They are defined in the class they belong to, and although it does look ugly, it's also easy to maintain.

The Page Object Model design pattern is designed to keep everything related to the "page" in one place. Moving these around to different classes just for house keeping sort of breaks that model as described here from the link: "Subsequently all changes to support that new UI are located in one place."

Related

Correct way to add a print functionality to a java class [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Lets say I have class Report and I want to add a functionality printReport(...) and shouldBePrinted(...). Printing it requires GeneralPrinter and LanguageTranslator which are given from outside. Furthermore, I should add members to make the shouldBePrintable method more optimized.
The way I see it there are three ways of doing it:
The simplest is to just add the members and functions to the Report class.
Create PrintableReport which extends Report and adds those members and functions.
Use the decorator pattern to add the needed functionality. (Not sure about that one. Please correct me if this is not the correct way to use a decorator.)
Am I missing some and which is the correct method to do it?
Consider: Separation of concerns
At a HIGH level...
While it's not clear exactly what role Report fills, one might surmise it represents information organized in some fashion.
Rendering is a separate concern. Often you'll want multiple ways to render: Generate PDF, HTML, XML, and/or print (postscript, other...).
So, perhaps you have multiple classes to work with Report, GeneralPrinter, ReportPrinter, ...

disadvantages for having a class with many fields [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a class that has many fields made of objects of other classes. This class is used by multiple people who keep adding more fields to it according to their needs. I want to know if there's a drawback to this compared to having one collection field, say a Hashmap, in this class which can be used to contain other classes as and when necessary. This looks cleaner to me than declaring many fields which might end up not being used
A class with too many fields and methods is certainly harder to grasp and change later on - the shorter the class is, the easier it is to understand its uses.
On the other hand, keeping different class variables inside one hashmap in order to make the class shorter is not a good idea at all because you will lose type safety and will have to add many additional checks and castings later on.
In conclusion you should always keep the classes as simple and clean as possible without sacrificing best coding practices - perhaps instead of having so many different fields in one class you could have multiple smaller classes, each with their own responsibility, instead.

how to implement the key words methods in selenium according to page when framework is keyword driven and data driven [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am using a framework which is key word driven and data driven in selenium.
The problem is all the methods or actions for entire application is written in one single class which has gotten very lengthy and confusing like spaghetti.
I want to implements all the methods or actions page wise like a page object model but I also want it to be data driven and key word driven as well.
Any suggestions please??
#Bryan Oakley
Your comment made me think harder which helped me finding the solution .
returnedStatus = runReflectionMethod("com.dmainc.ptes.test.setup.KeyWord", methodName, paramListObject);
resultSet.add(returnedStatus);
excelSheet.setCellData(filePath, sheetName, "Result", row + 1, returnedStatus);
So If **runReflectionMethod("com.dmainc.ptes.test.setup.KeyWord", methodName, paramListObject);**
com.dmainc.ptes.test.setup.KeyWord this part needs to be variable rather then constant which its now.
so if I make it variable and I create various classes page wise which will include the respective methods/action then I can pass the required class name in this piece of code .My problem will be solved .
All the methods/actions will go into their respective classes and no code bloating.
Since your framework is keyword and data-driven, it should support keyword mapping, for example Robot framework:
a dictionary mapping all library names to instances
which means that you don't need to worry too much about the refactoring of the
all the methods or actions for entire application is written in one single class
This is a classical example of a Bloater - Large Class. There are a lot of resources out there, but would recommend starting with
Refactoring To Patterns and sourcemaking.

Java: Is creating a "System" class a bad thing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.

Organizing Code in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
my main.java file has a length of about 1000 lines. My code is getting more and more confused, and I would like to "split" it in different parts (e.g. in one file I would have essential stuff like OnCreate, in another file I would have for instance GetHttpRequest).
I already tried to put GetHttpRequest in a different class, but is there no simpler way? (It would take a really long time to adjust the code if I used this method)
You have to use classes and methods, and optionally packages.
This will solve your problem. There's no simpler way than that.
Please do not hard-code your program. There are several patterns on how to code a program, so it is efficient, everybody can easily read and understand it. I think you also have a "GUI", assuming to this, I recommend you to use the MVC pattern. It means Model-View-Controller, so you organize your program in Packages: "model", "view", "controller" and in those packages you put the classes. For instance, you have a simple Calculator. Then you have a class in view thats called "CalculatorView", where your graphical interface is and in controller you have your "CalculatorController" that works out the things like calculations. (You call the controller from the view) and you do not need model at all.
I hope that helps you. But you will have to rewrite all your code...

Categories

Resources