Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const lodashArray = require('lodash/array');
- const webdriverio = require('webdriverio');
- const options = {debug:true,desiredCapabilities: { browserName: 'chrome', platform: 'XP'} };
- const client = webdriverio.remote(options);
- const async = require('async');
- var path = require('path');
- let loginURL = 'https://www.gumtree.pl/login.html';
- var add_title="Moje ogłoszenie 123";
- var request = require("request")
- class Gumtree {
- static login(user) {
- console.log("login: "+user.username);
- console.log("Password:"+user.password);
- console.log("URL:" +loginURL);
- return new Promise((resolve, reject) => {
- client
- .init()
- .url(loginURL)
- .pause(500)
- .setValue('#login input[name=email]', user.username)
- .pause(500)
- .setValue('#login input[name=password]', user.password)
- .pause(500)
- .click('#login-button')
- .pause(500)
- .call(() => {
- let url = client.getUrl();
- let tmpInterval = setInterval(() => {
- async.parallel({
- url: (callback) => {
- client.getUrl().then(url => {
- callback(null, url);
- })
- },
- loginButtonExist:(callback) =>{
- client.isExisting('#login-button').then(exist => {
- callback(null, exist);
- });
- }
- },
- (err, results) => {
- if (err) {
- reject(err);
- }
- if (results.loginButtonExist === false) { //logowanie sie powiodlo bo zniknal button
- console.log('login Successfull');
- resolve();
- clearInterval(tmpInterval);
- }else{
- reject('unsuccessful login, bad/password probably');
- clearInterval(tmpInterval);
- }
- });
- }, 500);
- });
- })
- }
- static gotoads() {
- return new Promise((resolve, reject) => {
- client
- .url('http://www.gumtree.pl/my/ads.html')
- .pause(500)
- .call(() => {
- resolve();
- });
- })
- }
- static add_ad() {
- console.log("ADD_AD");
- return new Promise((resolve, reject) => {
- client
- .url('https://www.gumtree.pl/post.html')
- .pause(500)
- .click('li[data-id="9"]').call(() => {
- // od tego momentu na rozne sposoby chce kliknac podkategorie, a potem wybor wojewodztwa i miasta, reszte sobie poradze bo to dziala jak jakims
- // cudem tam dotarlem tym slabym kodem nizej nizej ;d
- var isVisible = client.isVisible('li[data-id="9105"]').then(function (k) {
- console.log(k);
- });
- console.log(isVisible);
- }).then(function(){
- client.waitUntil(() => {
- var isVisible = this.isVisible('li[data-id="9105"]');
- console.log(isVisible);
- return isVisible;
- }, 100000, '')}).pause(10000).then(function(){
- console.log("is visible");
- this.click('li[data-id="9105"]');
- var subcategory_click = false;
- client.waitForVisible('li[data-id="9105"]');
- let mainInterval = setInterval(() => {
- this.pause(500);
- this.isExisting('li[data-id="9105"]').then(isVisible => {
- console.log("is existing"+isVisible);
- // this.click('li[data-id="9105"]');
- });
- client.waitForVisible('li[data-id="9105"]');
- var isVisible = this.isVisible('li[data-id="9105"]');
- console.log(isVisible);
- // console.log(subcategory_click);
- this.click('li[data-id="9105"]').pause(500);
- if (isVisible === true && !subcategory_click) {
- console.log('li[data-id="9105"] visible')
- this.click('li[data-id="9105"]').pause(500);
- subcategory_click = true;
- }
- },1000);
- })
- .pause(1500)
- })
- }
- static add_ad_old() {
- console.log("ADD_AD");
- return new Promise((resolve, reject) => {
- client
- .url('https://www.gumtree.pl/post.html')
- .pause(500)
- .click('li[data-id="9"]').then(function(){
- var subcategory_click = false;
- client.waitForVisible('li[data-id="9105"]')
- .then(function(visible){
- console.log("waitforvisible");
- console.log("visible: "+visible);
- });
- let mainInterval = setInterval(() => {
- this.isExisting('li[data-id="9105"]').then(isVisible => {
- if (isVisible === true && !subcategory_click) {
- console.log('li[data-id="9105"] visible')
- this.click('li[data-id="9105"]').pause(500);
- subcategory_click = true;
- }
- });
- },10000);
- })
- .pause(1500)
- .waitForVisible('li[data-id="9105"]')
- .then(function(visible){
- console.log(visible);
- console.log("is isible now");
- //this.click('li[data-id="9105"]').pause(500);
- this.url('https://www.gumtree.pl/post.html')
- this.click('li[data-id="9105"]').pause(500);
- this.pause(1500);
- // if (this.alertText()) {
- // this.alertAccept();
- // }
- //this.click('li[data-id="9105"]').pause(500);
- console.log("przed visible");
- /*
- var elem = this.element("#locSelector").then(function(cos) {
- console.log("elem:");
- console.log(elem);
- console.log(cos);
- console.log(this);
- });
- console.log("elem:");
- console.log(elem);
- */
- var firstCategoryClicked = false;
- this.alertAccept().then(function() {
- console.log("alert accept");
- client.pause(1000);
- let tmpInterval = setInterval(() => {
- console.log("Interval");
- if(client.isVisible('li[data-id="3200002"]') && !firstCategoryClicked) {
- console.log("data is viisible!");
- this.click('li[data-id="3200002"]');
- client.pause(1000);
- firstCategoryClicked = true;
- }
- if(client.isVisible('li[data-id="3200002"] .nav-cont') && client.isVisible('li[data-id="3200291"]')) {
- console.log("3200291 is viisible!");
- this.click('li[data-id="3200291"]');
- client.pause(1000);
- }
- if (client.isExisting('#title_ctnr') && client.isVisible('#title_ctnr')) {
- console.log("title is visible");
- }
- client.isExisting('input[name="Title"]').then(isExisting => {
- if (isExisting === true) {
- client.setValue('input[name="Title"]', add_title);
- console.log("Setting title");
- }
- });
- client.isExisting('#pictures').then(isExisting => {
- if (isExisting === true) {
- var toUpload = path.join(__dirname, '..', '..', 'icon-image-128.png');
- var toUpload = "C:\\Users\\michal\\Downloads\\tokchuju\\garage_attachment_zpscb730d13.jpg";
- console.log(toUpload);
- this.chooseFile('#pictures', toUpload);
- clearInterval(tmpInterval);
- }
- });
- },10000);
- this.waitForVisible('li[data-id="3200002"]').then(function (visible) {
- console.log("wojewodztwa visible");
- this.pause(500).click('li[data-id="3200002"]').pause(500);
- //this.click('li[data-id="3200311"]').pause(500);
- //this.url("https://www.gumtree.pl/my/profile.html");
- });
- console.log('sleep');
- client.pause(10000);
- this.pause(500).click('li[data-id="3200002"]').pause(500);
- });
- })
- })
- }
- }
- function GumtreeInit() {
- var user = {username:"", password:""};
- Gumtree.login(user).then().then(Gumtree.add_ad).then();
- return;
- exit;
- }
- async function asyncTimeout(time) {
- return new Promise((resolve, reject) => {
- setTimeout(()=>{
- resolve();
- }, time);
- })
- }
- async function letsgo(ad) {
- await client
- .url("https://www.gumtree.pl/post.html");
- await client.click('[data-id="'+ad.gumtree_category_1_level+'"]');
- while(true){
- try {
- const existing = await
- client.isExisting('[data-id="'+ad.gumtree_category_2_level+'"]');
- console.log("existing: " + existing);
- if (existing) {
- const visible = await
- client.isVisible('[data-id="'+ad.gumtree_category_2_level+'"]');
- console.log("visible: " + visible);
- if (visible) {
- await
- client.click('[data-id="'+ad.gumtree_category_2_level+'"]');
- break;
- }
- }
- }catch(Exception){
- }
- await asyncTimeout(1000);
- }
- console.log("klikniete kategorie ");
- while(true){
- const existing = await client.isExisting('[data-id="'+ad.gumtree_region_1_level+'"]');
- console.log("existing: " + existing);
- if (existing) {
- const visible = await client.isVisible('[data-id="'+ad.gumtree_region_1_level+'"]');
- console.log("visible: " + visible);
- if (visible) {
- await client.click('[data-id="'+ad.gumtree_region_1_level+'"]');
- break;
- }
- }
- await asyncTimeout(1000);
- }
- while(true){
- const existing = await client.isExisting('[data-id="'+ad.gumtree_region_2_level+'"]');
- if (existing) {
- const visible = await client.isVisible('[data-id="'+ad.gumtree_region_2_level+'"]');
- if (visible) {
- await client.click('[data-id="'+ad.gumtree_region_2_level+'"]');
- break;
- }
- }
- await asyncTimeout(1000);
- }
- console.log("region wybrany");
- while(true){
- const existing = await client.isExisting('input[name="Title"]');
- if (existing) {
- const visible = await client.isVisible('input[name="Title"]');
- if (visible) {
- await client.setValue('input[name="Title"]', ad.title);
- break;
- }
- }
- await asyncTimeout(1000);
- }
- if(ad.for_rent_by){
- }
- console.log("Tytul wpisany");
- while(true){
- const existing = await client.isExisting('#description-frame');
- if (existing) {
- const visible = await client.isVisible('#description-frame');
- if (visible) {
- await client.click('#description-frame');
- await client.keys(ad.description);
- break;
- }
- }
- await asyncTimeout(1000);
- }
- console.log("opis wpisany");
- while(true){
- const existing = await client.isExisting('select[name="DwellingType"]');
- if (existing) {
- const visible = await client.isVisible('select[name="DwellingType"]');
- if (visible) {
- await client.selectByValue('select[name="DwellingType"]', ad.dwelling_type);
- break;
- }
- }
- await asyncTimeout(1000);
- }
- console.log("Rodzaj nieruchomosci wybrany");
- }
- async function zrobpetleresult(result) {
- await letsgo(result);
- }
- async function inicjuj(){
- client.init().then(function (k) {
- var url = "http://localhost:8000/api/ads";
- request({
- url: url,
- json: true
- }, function (error, response, body) {
- if (!error && response.statusCode === 200) {
- for (let result of body)
- {
- console.log(result);
- await zrobpetleresult(result);
- //break;
- }
- ;
- }
- });
- return;
- });
- }
- inicjuj();
- return;
- letsgo();
- return;
- GumtreeInit();
- return;
- exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement