java, selenium,selenide: adding random product to the cart (stream) - selenide

i am using (java, selenium,selenide). there is a list of product div on a page. it contains product information, price and buy button. i need to randomly select 2 products that have price < 290 and click on buy button in the corresponding div. help please. it is important.
private final String parent = "//div[#class ='catalog-item']"; //div-ы
private final String childCost = "/div[#class='catalog-item-store']/div[#class='catalog-item-price']";//переход от div к цене
public void randomSelectionElement() {
// ArrayList upto290 = new ArrayList<>();
List listCardsGoods = $$x(parent);//лист дивов
SelenideElement cost = $x(parent + childCost); //перешли к элементу Цена
ElementsCollection costList = $$x(parent + childCost);//коллекция цен
List sortCost = costList.stream().filter(SelenideElement -> Integer.valueOf(cost.getText().replace(" руб", "").replace(",", ".")) < 290).);
Random rand = new Random();
for (int i = 0; i < 1; i++) {
int randomIndex = rand.nextInt(sortCost.size());//выбираем рандомный индекс
sortCost.get(randomIndex);//берем элемент
//взять xpath этой цены и перейти к его кнопке Купить
sortCost.remove(randomIndex);
}

Related

Calulate two random numbers in flutter

I'm trying to generate two different random numbers and add those together, but Flutter doesn't seem to like my math. I keep getting the message that '+' isn't defined for the class Random.
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MaterialApp(
title: 'Random Numbers',
theme: ThemeData(primarySwatch: Colors.orange),
home: MyHome(),
));
class MyHome extends StatefulWidget {
#override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> {
#override
Widget build(BuildContext context) {
var num1 = new Random();
for (var i = 0; i < 10; i++) {
print(num1.nextInt(10));
}
var num2 = new Random();
for (var i = 0; i < 10; i++) {
print(num2.nextInt(10));
}
//var sum = num1 + num2;
return Container();
}
}
My goal is to display it something like this: "2 + 5 = "
where the user will fill in the answer. If correct do this else do that.
The error is telling you that you're trying to add two Random objects, and not two numbers. You're printing them correctly, using nextInt() on your loops, but when you try to sum them, you're using the original variable of the type Random.
Try this:
class _MyHomeState extends State<MyHome> {
#override
Widget build(BuildContext context) {
// Instantiate a Random class object
var numGenerator = new Random();
//You don't need a second loop because it was the same exact code,
//only with a different variable name.
for (var i = 0; i < 10; i++) {
print(numGenerator.nextInt(10));
}
// Save the numbers you generated. Each call to nextInt returns a new one
var num1 = numGenerator.nextInt(10);
var num2 = numGenerator.nextInt(10);
var sum = num1 + num2;
//use num1, num2 and sum as you like
return Container();
}
}
Thank you very much Gerorge and Sorry for my abscense.
I got some help to solve this through dart
Random seed = Random();
const int MAX_VALUE = 10;
int val1 = seed.nextInt(MAX_VALUE);
int val2 = seed.nextInt(MAX_VALUE);
int sum = val1 + val2;
print('$val1 + $val2 = $sum');

C#. Search array of strings for longest element

I want to know how I can get out everyone of the the longest persons if there are several with the same length?
If only one person is the longest, then it works fine and the longest person with it´s name will show in MessageBox. But if there are more than one who are the longest, this code will not work...
public partial class Form1 : Form
{
int[] längdArray = new int[5];
string[] namnArray = new string[5];
int namn = 0;
int längd = 0;
public Form1()
{
InitializeComponent();
}
private void btnVisa_Click(object sender, EventArgs e)
{
int längst = 0;
int längdvärdet = 0;
int längdindex = 0;
string name = textBox1.Text;
namnArray[namn] = name;
namn = namn + 1;
textBox1.Clear();
int centimeter = int.Parse(textBox2.Text);
längdArray[längd] = centimeter;
längd++;
textBox2.Clear();
listBox1.Items.Add(name + " " + centimeter + " centimeter ");
if (längd == 5)
{
btnVisa.Enabled = false;
foreach (int antalLängder in längdArray)
{
if (antalLängder > längst)
{
längst = antalLängder;
längdvärdet = längdindex;
}
längdindex++;
}
string test = namnArray[längdvärdet]
MessageBox.Show(" Längsta person är " + test + " som är " + längst + " centimeter lång ");
}
Define behavior you want your app to present when there is more than one person. Should all display, or any one, or other? Try to use object constructions, it's easier to operate on them. C# is an object-oriented language. Put name and length in one structure then use LINQ.

Validating user's input while using a while loop

My issue is that I'm trying to validate my input by making sure that the while loop only takes a number more than 10. If not, they have to try again. But the issue I'm having is that the loop keeps repeating itself if I close the while loop.
import java.util.Scanner;
/**
* #(#)Hotel_Occupancy.java
*
* Hotel_Occupancy application
*
* #author
* #version 1.00 2013/10/28
*/
public class Hotel_Occupancy {
public static void main(String[] args) {
//Declare Variables
int floors;
int rooms;
int roomsOccupied;
int roomsVacant;
int occupancyRate;
//Create Scanner Object
Scanner keyboard = new Scanner(System.in);
//Ask user for # of floors
System.out.print("Enter floors? Input number more than 1. ");
floors = keyboard.nextInt();
while (floors < 1 && floors != 10 )
{
System.out.println("ENTER VALID NUMBER.");
System.out.println("Enter floors? Input number more than 1. ");
System.;
//Create loop with floor iteration
for (rooms = 1; rooms <= floors; rooms++)
{
System.out.println("How many rooms? ");
rooms = keyboard.nextInt();
}
for (roomsOccupied = 1; roomsOccupied <= rooms; roomsOccupied++)
{
System.out.println("How many rooms occupied? ");
roomsOccupied = keyboard.nextInt();
roomsVacant = rooms - roomsOccupied;
System.out.println("Rooms Vacant" + roomsVacant);
}
//Calculate Occupancy Rate
occupancyRate = roomsOccupied + rooms;
System.out.println("Occupancy rate is " + occupancyRate);
}
}
}
while(floor >=10){
// Do whatever you want
}
Go through this basic while loop tutorial.

How can I post a list then access it in my controller?

I created a list property in my model like so
public virtual List<String> listOfDays { get; set; }
then I converted and stored it in the list like this:
for (int i = 0; i < 30 i++)
{
var enrollment = new Enrollment();
enrollment.StudentID = id;
enrollment.listOfDays = searchString.ToList();
db.Enrollments.Add(enrollment);
db.SaveChanges();
}
I put a breakpoint here... enrollment.listOfDays = searchString.ToList(); ... and all is well. I see that the conversion was performed and I can see the values in listOfDays.
So I thought I would find a column in my database called listOfDays since I'm doing code first but the property is not there.
Then I thought I'd try accessing it anyway like this...
var classdays = from e in db.Enrollments where e.StudentID == id select e.listOfDays;
var days = classdays.ToList();
//here I get an error message about this not being supported in Linq.
Questions:
Why do you think the property was not in the database?
How can I post this array to my model then access it in a controller?
Thanks for any help.
Thanks to Decker: http://forums.asp.net/members/Decker%20Dong%20-%20MSFT.aspx
Here’s how it works:
Using form collection here…
In [HttpPost]…
private void Update (FormCollection formCollection, int id)
for (int sc = 0; sc < theSelectedCourses.Count(); sc++)
{
var enrollment = new Enrollment();
enrollment.CourseID = Convert.ToInt32(theSelectedCourses[sc]);
enrollment.StudentID = id;
enrollment.listOfDays = formCollection["searchString"] ;//bind this as a string instead of a list or array.
Then in [HttpGet]…
private void PopulateAssignedenrolledData(Student student, int id)
{
var dayList = from e in db.Enrollments where e.StudentID == id select e;
var days = dayList.ToList();
if (days.Count > 0)
{
string dl = days.FirstOrDefault().listOfDays;
string[] listofdays = dl.Split(',');
ViewBag.classDay = listofdays.ToList();
}
Thanks to Decker: http://forums.asp.net/members/Decker%20Dong%20-%20MSFT.aspx
Here’s how it works:
Using form collection here…
In [HttpPost]…
private void Update (FormCollection formCollection, int id)
for (int sc = 0; sc < theSelectedCourses.Count(); sc++)
{
var enrollment = new Enrollment();
enrollment.CourseID = Convert.ToInt32(theSelectedCourses[sc]);
enrollment.StudentID = id;
enrollment.listOfDays = formCollection["searchString"] ;//bind this as a string instead of a list or array.
Then in [HttpGet]…
private void PopulateAssignedenrolledData(Student student, int id)
{
var dayList = from e in db.Enrollments where e.StudentID == id select e;
var days = dayList.ToList();
if (days.Count > 0)
{
string dl = days.FirstOrDefault().listOfDays;
string[] listofdays = dl.Split(',');
ViewBag.classDay = listofdays.ToList();
}

Iterate Form Fields in WatiN

Is there a way to iterate all form field in WatiN? Code snippet would be highly appreciated. Thanks.
string url = "http://localhost//test.htm";
string formId = "myFormId";
IE ie = new IE(url);
Form form = ie.Form(formId);
CheckBoxCollection checkBoxCollection = form.CheckBoxes;
for (int index = 0, total = checkBoxCollection.Count; index < total; index++) {
CheckBox checkBox = checkBoxCollection[index];
this.AddInputControl(checkBox.Id, checkBox);
}
RadioButtonCollection radioButtonCollection = form.RadioButtons;
for (int index = 0, total = radioButtonCollection.Count; index < total; index++) {
RadioButton radioButton = radioButtonCollection[index];
this.AddInputControl(radioButton.Id, radioButton);
}
SelectListCollection selectListCollection = form.SelectLists;
for (int index = 0, total = selectListCollection.Count; index < total; index++) {
SelectList selectList = selectListCollection[index];
this.AddInputControl(selectList.Id, selectList);
}
TextFieldCollection textFieldCollection = form.TextFields;
for (int index = 0, total = textFieldCollection.Count; index < total; index++) {
TextField textField = textFieldCollection[index];
this.AddInputControl(textField.Id, textField);
}
Try this:
string url = "http://localhost//test.htm";
string formId = "myFormId";
IE ie = new IE(url);
Form form = ie.Form(formId);
ElementCollection elements = form.Elements.Filter(Find.ByElement(x =>
{
return x is TextField || x is SelectList || x is RadioButton || x is CheckBox;
}));

Resources