Is it better to pass in $event vs implying? [closed] - vue.js

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
Is it better practice to pass in the event when using it in a method or to just have it implied:
option1:
<input #keyup.enter="chooseMe($event)"/>
option2:
<input #keyup.enter="chooseMe"/>
chooseMe(evt) {
console.log(evt)
}

I'll assume that you are asking about performance rather than developer opinion. (For example, it could be argued that explicitly passing the $event argument makes the component's intention clearer, but that's an opinion. It's also more verbose.)
The compiler generates a wrapper function around any handler that uses a function invocation (i.e. () in the attribute value), to allow for passing $event as an argument. When using modifiers like .enter in your example, it does this even if there is no invocation.
To test any performance impact, you can use a test suite like JSBench.me. Here are the results of such a test running on Windows/Chrome.
Implicit
<div #click="test"></div>
Result:: ~502,653 operations per second.
Explicit
<div #click="test($event)"></div>
Result:: ~536,685 operations per second.
It's faster (by an imperceptible margin) to include the $event in this case. But the difference is insignificant enough to be irrelevant. If you had to compile 500,000 event handlers, the act of compilation for both syntaxes would take around 1 second.

In Option1 you create an expression each time the component gets rendered. But, in Option2 in each cycle, just one method is addressed. So, it's better to define methods and address them in the template.
Consider this sample:
<list>
<li
v-for="item in items" :key="item.id"
#click="theClickHandler(item)"
></li>
</list>
There are a theClickHandler method and item.length different expressions in each render (at most)!!
To handle this situation you can use HTML data attributes:
<list>
<li
v-for="item in items" :key="item.id"
:data-id="item.id"
#click="theClickHandler"
></li>
</list>
and get the clicked item in hanlder method:
theClickHandler(e) {
const id = e.target.dataset.id
const [item] = this.items.filter(x => x.id === id)
console.log('clicked item:', item)
},

Related

Vue 2 - How / is it possible to create a unique layout during a v-for loop?

I am trying to create a questionnaire. I have an array of questions. Each question is an object. During the loop the <component :is> checks the component property inside the question object. If the property equals an Input for example then an input will be shown and so on.
This works for simple questions. However the last question requires a more complex layout. Here 'Please add items' needs to have two inputs and an add button. Once pressed a table will appear with each row displaying the values passed into the fields from above. At the moment I can't do this as I am looping though a sub set of questions.
There could be 1000+ questions eventually and I am not sure whether creating a component for each question is the right approach?
I know my current approach isn't right some how but I am completely stuck how to approach this. Is there a way of looping through data and providing unique layouts for each question? The data structure isn't set in stone so feel free to change it.
https://codesandbox.io/embed/blazing-wood-ifnxym?fontsize=14&hidenavigation=1&theme=dark
Within the v-for, you can wrap the elements in a <template>, and then just use v-if to determine which element is displayed.
For example :
<template v-for="question in question.questions" :key="question.id">
<Input v-if="quetion.type === 'text'" :question="question" />
<Radio v-if="quetion.type === 'choice'" :question="question" />
...
</template>

Display one object property value, and show another one on hover

the question I have might be hard to understand, so please help me re-organize the question if you can see the better way to put it in.
So, I am building a registration platform.
(1) First, I receive an array of objects of cases the user can sign time to.
(2) Each object consists of 2 properties, "name", "description".
(3) I store the array in the data, end use it in the element provided by a picker called "vue-multiselect", which basically accepts the array and loops over the objects and displays them.
(4) As you can see, it displays both properties and values, which I am trying to avoid. My question is, is it possible to pass only the "name" value into the picker, and display the description value when hovering the first value?
You can find this use case documentation here: https://vue-multiselect.js.org/#sub-custom-option-template
<multiselect v-model="value"
deselect-label=""
select-label=""
placeholder=""
selected-label=""
:show-pointer="true"
:options="projectCases">
<template slot="option" slot-scope="{ option }">
<strong :title="option.description">{{ option.name }}</strong>
</template>
</multiselect>
ps: I use title attribute to implement display-on-hover functionality. you can use any other tooltip library as well.

get complete Vuetify item from v-autocomplete

I need to get the entire array element that makes up the Autocomplete item, I want the element so I can store it in Veux without store each item, such as first name, MI last name etc. I'm going to build upon another question that was very similar that was posted here Vuetify v-select get item index and with the jsfiddle answer solution
my code that I'm using is:
<v-list-tile>
<v-autocomplete
v-model="data"
:allow-overflow="false"
:items="named_items"
:item-text="getFullName"
:loading="loadingMembers"
:debounce-search="0"
:search-input.sync="searchInput"
class="purple-input search-input"
default
color="purple"
autofocus
placeholder="Search..."
item-value="MemberID"
hide-no-data
no-data-text="Add New Member"
#change="changeMember"
#keyup.enter="hitEnter"
/>
</v-list-tile>
the jsfiddle is at https://jsfiddle.net/Roland1993/fg461d55/1/
and my question is when an item is selected is it possible to get the element that makes up the ID, Question and Answers? so the it can be stored as an array?
It is entirely possible, for example, you have already bound to events, but you can have the name or values of their targets.
See: Method Event handlers

Artificial Intelligence in UI Automation testing [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 4 years ago.
Improve this question
I am currently working on a project where the xpath of the elements changes continuously. At the UI display level there is not much visible change in UI, however the DOM elements changes continuously.
I am thinking of a better approach to handle failures in test case due to minor changes in DOM structure. I am currently using Selenium with TestNg framework for UI Automation Testing.
Any suggestions or directions on alternate approach would be helpful.
Are you following the orders of locators suggested by Selenium contributors: if not then please follow this order :
ID
name
className
linkText
partialLinkText
tagName
cssSelector
XPath.
Note : Most of the time a cssSelector can replace Xpath, However Xpath has its own advantages which cssSelector do not provide.
For more reference you can go through this SO Link : Css vs Xpath
My suggestion would be:
1. Try using different locators for particular element[https://api.jquery.com/multiple-selector/]
something like selector1, selector2, selectorN. If selector1 is not available in DOM, control will not throw an error instead, it will search selector2 and so on
2. Use Explicit waits
I use By.CSS selectors rather than by xpath, as these are less prone to changes in the DOM.
So for this example dom:
<div class="smc-login-container">
<form role="form" action="/login.html" method="POST" name="login" class="ng-pristine ng-valid">
<!-- Username -->
<label for="username">User ID:</label>
<input type="text" class="smc-login-control aftLoginUsernameField" id="username" name="username">
<!-- Password -->
<label for="password">Password:</label>
<input type="password" class="smc-login-control aftLoginPasswordField" id="password" name="password">
<!-- Cross site scripting token -->
<input type="hidden" id="_csrf" name="_csrf" value="efaa05c4-77a8-443d-9484-51e8c9795c28">
<!-- Sign In Button -->
<button id="signIn" class="btn btn-lg btn-block smc-login-button aftLoginSignInButton">
Sign In
</button>
</form>
These selectors work:
Example 1. Find Button, element type and class
WebElement element = webDriver.findElement(By.cssSelector("button.btn"));
Example 2. Find Button, direct child rather than descendent
Uses full DOM and thus form element:
WebElement element = webDriver.findElement(By.cssSelector("div > form > button"));
Doesn't use form element:
WebElement element = webDriver.findElement(By.cssSelector("div button"));
Example 3. Find Input following the username label, attribute with value then it’s sibling (+)
WebElement element = webDriver.findElement(By.cssSelector("label[for='username'] + input"));
Example 4. Find Password input and Button, OR’d (comma) returning more than one element
List<WebElement> elements;
elements = webDriver.findElements(By.cssSelector("input.smc-login-control.aftLoginPasswordField , .btn"));
elements.get(0).sendKeys("my password");
elements.get(1).click();
Example 5. Find username input, has class attribute, but not attribute name = ‘password’:
WebElement element = webDriver.findElements(By.cssSelector("input[class]:not([name='password'])"));
Another thing I do is use a more generic pattern, which finds multiple items. I've found the items are always found in the same sequence and thus I can access the one I want by just using the array index (like in example 4).
Finally, if the DOM has dynamic values I've found that I can use a specific stable pattern to find a parent element which has the same dynamic value, thus extract it and then reuse it to find the other element I actually want.

An attribute for all checkbox in page is changing each time I run the code, how to find element for checkbox [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Refer this firepath :
.//*[#id='gwt-uid-20138']
HTML :
<input id="gwt-uid-20138" type="checkbox" value="on" tabindex="0">
Note: this id value gets changed.
If id is changing, maybe try to find checkbox using label next to this checkbox?
Try something like this:
/label[text()="test"]/preceding-sibling::input
If selector of your checkbox keeps on changing you need to identify it with the property which is always constant and unique.
From your example you can see that gwt-uid- part is always constant, you can use the same to identify the element.
driver.findElement(By.xpath("//input[starts-with(#id, 'gwt-uid-')]"));
To find the checkbox element you have to take help of the id attribute. But as you can see the id attribute is dynamic, so we have to make our Locator Strategy dynamic. Now if you observe the id attribute closely you will observe that the gwt-uid- part is constant and only the remaining part i.e. 20138 is dynamic. So we can use the starts-with() method for the dynamic id and to identify the checkbox uniquely we will also include the type attribute while constructing the locator. So to find the checkbox element you can use the following line of code :
Java :
WebElement element = driver.findElement(By.xpath("//input[starts-with(#id,'gwt-uid-') and #type='checkbox']"));